weinerm21

Untitled

Jun 11th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #include <Servo.h>
  2. Servo myServo;
  3. const int trigPin = 12;
  4. const int echoPin = 11;
  5. const int ledPinGreen = 6;
  6. const int ledPinYellow = 5;
  7. const int ledPinRed = 4;
  8. long duration;
  9. int distance;
  10. int potVal;
  11. int angle;
  12.  
  13. void setup()
  14. {
  15. // put your setup code here, to run once:
  16. pinMode(trigPin, OUTPUT);
  17. pinMode(echoPin, INPUT);
  18. pinMode(ledPinRed, OUTPUT);
  19. pinMode(ledPinYellow, OUTPUT);
  20. pinMode(ledPinGreen, OUTPUT);
  21. myServo.attach(9);
  22. Serial.begin(9600);
  23. }
  24.  
  25. void loop()
  26. {
  27. // put your main code here, to run repeatedly:
  28. digitalWrite(trigPin, LOW);
  29. delayMicroseconds(2);
  30. digitalWrite(trigPin, HIGH);
  31. delayMicroseconds(10);
  32. digitalWrite(trigPin, LOW);
  33. duration = pulseIn(echoPin, HIGH);
  34. distance= duration*0.034/2;
  35. Serial.print("Distance: ");
  36. Serial.println(distance);
  37. if(distance <=10)
  38. {
  39. digitalWrite(ledPinRed, HIGH);
  40. digitalWrite(ledPinYellow, LOW);
  41. digitalWrite(ledPinGreen, LOW);
  42. myServo.write(90);
  43. }
  44. if(distance > 10 && distance <=20)
  45. {
  46. digitalWrite(ledPinRed, LOW);
  47. digitalWrite(ledPinYellow, HIGH);
  48. digitalWrite(ledPinGreen, LOW);
  49. myServo.write(180);
  50. }
  51. if(distance > 20)
  52. {
  53. digitalWrite(ledPinRed, LOW);
  54. digitalWrite(ledPinYellow, LOW);
  55. digitalWrite(ledPinGreen, HIGH);
  56. myServo.write(180);
  57. }
  58. }
Add Comment
Please, Sign In to add comment