safwan092

Untitled

May 30th, 2023
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. #define trigPin 13
  2. #define echoPin 12
  3. #define led 11
  4. void setup()
  5. {
  6. Serial.begin (9600);
  7. pinMode(trigPin, OUTPUT);
  8. pinMode(echoPin, INPUT);
  9. pinMode(led, OUTPUT);
  10. }
  11. void loop()
  12. {
  13. long duration, distance;
  14. digitalWrite(trigPin, LOW);
  15. delayMicroseconds(2);
  16. digitalWrite(trigPin, HIGH);
  17. delayMicroseconds(10);
  18. digitalWrite(trigPin, LOW);
  19. duration = pulseIn(echoPin, HIGH);
  20. distance = (duration/2) / 29.1;
  21. if (distance < 10)
  22. {
  23. digitalWrite(led,HIGH);
  24. }
  25.  
  26. else
  27. {
  28. digitalWrite(led,LOW);
  29. }
  30.  
  31. Serial.print(distance);
  32. Serial.println(" cm");
  33. delay(500);
  34. }
Advertisement
Add Comment
Please, Sign In to add comment