Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. #define trigPin 7
  4.  
  5. #define echoPin 6
  6.  
  7. Servo servo;
  8.  
  9. int sound = 250;
  10.  
  11. void setup() {
  12.  
  13. Serial.begin (9600);
  14.  
  15. pinMode(trigPin, OUTPUT);
  16.  
  17. pinMode(echoPin, INPUT);
  18.  
  19. servo.attach(8);
  20.  
  21. }
  22.  
  23. void loop() {
  24.  
  25. long duration, distance;
  26.  
  27.  
  28. digitalWrite(trigPin, LOW);
  29.  
  30. delayMicroseconds(2);
  31.  
  32. digitalWrite(trigPin, HIGH);
  33.  
  34. delayMicroseconds(10);
  35.  
  36. digitalWrite(trigPin, LOW);
  37.  
  38. duration = pulseIn(echoPin, HIGH);
  39.  
  40. distance = (duration/2) / 29.1;
  41.  
  42. if (distance < 5) {
  43.  
  44. Serial.println("the distance is less than 5");
  45.  
  46. servo.write(90);
  47.  
  48. }
  49.  
  50. else {
  51.  
  52. servo.write(0);
  53.  
  54. }
  55.  
  56. if (distance > 60 || distance <= 0){
  57.  
  58. Serial.println("The distance is more than 60");
  59.  
  60. }
  61.  
  62. else {
  63.  
  64. Serial.print(distance);
  65.  
  66. Serial.println(" cm");
  67.  
  68. }
  69.  
  70. delay(500);
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement