zoukankan      html  css  js  c++  java
  • 用Tinkercad学arduino之 智能垃圾筒 超声波测距转动舵机

    项目地址:https://www.tinkercad.com/things/jFMVSYzlPx9-servo-motor-and-rgb-led-controlled-by-ultrasonic-sensor

    /*
      Homework 14-2a: 
        Servo Motor and RGB LED Controlled by Ultrasonic Sensor
      ENGR7A7B DUT-UCI Joint Program-Spring 2020
      Leo Zheng
      June 2 2020
    */
    
    /*
      The code can be used to control the water level in a cistern.
      The ultrasonic sensor is fixed over water. 
      Ultrasonic will reflect on the water surface.
      The depth of water is given by the distance from the sensor to the bottom 
      of the cistern, 4 meters minus the distance measured by the sensor.
      RGB LED is used as an alarm. 
      The servo motor controls a valve adding water to the cistern. 
      When the water level is too high, RGB LED will turn red and blink , 
      and the valve will close.
      When the water level is medium, RGB LED will turn yellow and blink, 
      and the valve will open a little. 
      When the water level is low, RGB LED will turn green, 
      and the valve will open .
    */
    
    #include <Servo.h>
    
    const int triggerPin=13, echoPin=12;
    const int redPin=10, bluePin=8, greenPin=7;
    int servoPWM, UltrasonicHeight=4;  
    //UltrasonicHeight is the distance from the sensor to the bottom of the cirtern.
    float depth;
    
    Servo servo_3;
     
    double UltrasonicDistance(int triggerPin, int echoPin) {   
      digitalWrite(triggerPin, LOW);   
      delayMicroseconds(2);   
      // Sets the trigger pin to HIGH state for 10 microseconds   
      digitalWrite(triggerPin, HIGH);   
      delayMicroseconds(10);   
      digitalWrite(triggerPin, LOW);   
      // Reads the echo pin, and returns the sound wave travel time in microseconds   
      long duration = pulseIn(echoPin, HIGH);
      return 0.000173225 * duration;  // at 25oC temperature 
    }
       
    //Print "The water depth is ... m":
    void depthPrint(){
      depth =UltrasonicHeight - UltrasonicDistance(triggerPin, echoPin);
      Serial.println();
      Serial.print("The water depth is ");   
      Serial.print(depth);
      Serial.println("m");
    }
    
    //Define Digital Pin Modes:
    void setup() {     
      servo_3.attach(3);
      pinMode(triggerPin, OUTPUT); 
      pinMode(echoPin, INPUT); 
      pinMode(redPin, OUTPUT); 
      pinMode(bluePin, OUTPUT); 
      pinMode(greenPin, OUTPUT); 
      Serial.begin(9600);   
    } 
    
    void loop() {      
      if (depth>3){                   //When the depth of water is larger than 3 meters.
        servo_3.write(0);             //Close the valve
        digitalWrite(greenPin, LOW);
        
        for (;depth>3;){
          depthPrint();
          Serial.println("DANGER! High Water Level.");
          
          digitalWrite(redPin,HIGH);  //Turn red and blink
          delay(250);
          digitalWrite(redPin,LOW);
          delay(250);
        }
      }
      else if (depth>=2.5&&depth<=3){ //When the depth of water is between 2.5 meters and 3 meters.
        servo_3.write(30);            //Open the valve 30 degrees
        
        for (;depth>=2.5&&depth<=3;){
          depthPrint();
          Serial.println("A LITTLE DANGER! Medium Water Level.");
          
          analogWrite(redPin, 255);   //Turn yellow and blink
          analogWrite(greenPin, 255);
          delay(250);
          digitalWrite(redPin,LOW);
          digitalWrite(greenPin,LOW);
          delay(250);
        }
      }
      else {                          //When the depth of water is smaller than 2.5 meters。
        depthPrint();
        Serial.println("SAFE! Low Water Level.");
        
        digitalWrite(redPin, LOW);
        digitalWrite(greenPin, HIGH); //Turn green
        
        servoPWM = (UltrasonicHeight-depth)*1800/33; 
        servo_3.write(servoPWM);  
        delay(30);                    //Open the valve(Degree depends on depth)
      }
    }
  • 相关阅读:
    【扫盲】i++和++i的区别
    java 字符串String.intern()方法学习
    随机访问和快速访问
    Semaphore信号量深度解析
    CyclicBarrier回环屏障深度解析
    CountDownLatch深度剖析
    静态代理和装饰者模式的区别
    AspectJ之@DeclareParents注解为对象添加新方法
    C#开发上位机常用
    使用Charles进行抓包、篡改请求、设置代理
  • 原文地址:https://www.cnblogs.com/meetrice/p/14100925.html
Copyright © 2011-2022 走看看