Advertisement
Erfinator

ping

Dec 29th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. const int pingPin = 7;
  2. void setup()
  3. {
  4. Serial.begin(9600);
  5. }
  6. void loop()
  7. {
  8. int duration, cm; // Variable for duration and cm
  9. pinMode(pingPin, OUTPUT); // setting pingPin as an OUTPUT for ultrasonic sensor outward pulse
  10. digitalWrite(pingPin, LOW); // short LOW state to ensure clean HIGH pulse
  11. delayMicroseconds(2);
  12. digitalWrite(pingPin, HIGH); // send outward pulse
  13. delayMicroseconds(5); // keep ultrasonic sensor at HIGH for 5 microseconds
  14. digitalWrite(pingPin, LOW); // turn ultrasonic sensor outwards pulse off
  15.  
  16. pinMode(pingPin, INPUT); // setting pingPin as an INPUT for ultrasonic sensor to recieve echo pulse
  17. duration = pulseIn(pingPin, HIGH); // variable duration to be
  18.  
  19. cm = (duration/2) /29.3866996;
  20.  
  21. Serial.print(cm);
  22. Serial.println(" cm");
  23. delay(100);
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement