Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. int trigger_pin = 11;
  2. int echo_pin = 10;
  3. double speed_of_sound = 0.0343;
  4.  
  5. void setup() {
  6. pinMode(trigger_pin, OUTPUT);
  7. pinMode(echo_pin, OUTPUT);
  8. Serial.begin(57600);
  9. }
  10.  
  11. void loop() {
  12. delayMicroseconds(8);
  13.  
  14. digitalWrite(trigger_pin, HIGH);
  15. delayMicroseconds(10);
  16. digitalWrite(trigger_pin, LOW);
  17.  
  18. long time_of_flight = pulseIn(echo_pin, HIGH);
  19.  
  20. // divide by two because flight time out
  21. // and flight time in is double distance
  22. long distance = (time_of_flight*speed_of_sound)/2;
  23.  
  24. Serial.println("Ultrasonic distance: " + distance);
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement