Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.24 KB | None | 0 0
  1.  
  2. int redled = 8;
  3. int yellowled = 7;
  4. int greenled = 6;
  5.  
  6. const int trigPin = 9;
  7. const int echoPin = 10;
  8.  
  9. long duration;
  10. int distance;
  11.  
  12. void setup() {
  13. pinMode(redled, OUTPUT);
  14. pinMode(yellowled, OUTPUT);
  15. pinMode(greenled, OUTPUT);
  16. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  17. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  18. Serial.begin(9600); // Starts the serial communication
  19. }
  20. void loop() {
  21. // Clears the trigPin
  22. digitalWrite(trigPin, LOW);
  23. delayMicroseconds(2);
  24. // Sets the trigPin on HIGH state for 10 micro seconds
  25. digitalWrite(trigPin, HIGH);
  26. delayMicroseconds(10);
  27. digitalWrite(trigPin, LOW);
  28. // Reads the echoPin, returns the sound wave travel time in microseconds
  29. duration = pulseIn(echoPin, HIGH);
  30. // Calculating the distance
  31. distance= duration*0.034/2;
  32. Serial.print("Distance: ");
  33. Serial.println(distance);
  34.  
  35.  
  36. if (distance <= 30){ //this
  37.   digitalWrite(redled, LOW);
  38.   digitalWrite(yellowled, LOW);
  39.   digitalWrite(greenled, HIGH);
  40. }
  41.  
  42. if (distance <= 20){ //this
  43.   digitalWrite(redled, LOW);
  44.   digitalWrite(yellowled, HIGH);
  45.   digitalWrite(greenled, LOW);
  46. }
  47.  
  48. if (distance <= 10){ //this
  49.   digitalWrite(redled, HIGH);
  50.   digitalWrite(yellowled, LOW);
  51.   digitalWrite(greenled, LOW);
  52. }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement