項目地址: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) } }