gnamp

ping))) code

Mar 28th, 2013
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. /* Ping))) Sensor
  2.  
  3. This sketch reads a PING))) ultrasonic rangefinder and returns the
  4. distance to the closest object in range. To do this, it sends a pulse
  5. to the sensor to initiate a reading, then listens for a pulse
  6. to return. The length of the returning pulse is proportional to
  7. the distance of the object from the sensor.
  8.  
  9. The circuit:
  10. * +V connection of the PING))) attached to +5V
  11. * GND connection of the PING))) attached to ground
  12. * SIG connection of the PING))) attached to digital pin 7
  13.  
  14. http://www.arduino.cc/en/Tutorial/Ping
  15.  
  16. created 3 Nov 2008
  17. by David A. Mellis
  18. modified 30 Aug 2011
  19. by Tom Igoe
  20.  
  21. This example code is in the public domain.
  22.  
  23. */
  24.  
  25. // this constant won't change. It's the pin number
  26. // of the sensor's output:
  27. const int pingPin = 7;
  28.  
  29. void setup() {
  30. // initialize serial communication:
  31. Serial.begin(9600);
  32. }
  33.  
  34. void loop()
  35. {
  36. // establish variables for duration of the ping,
  37. // and the distance result in inches and centimeters:
  38. long duration, inches, cm;
  39.  
  40. // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  41. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  42. pinMode(pingPin, OUTPUT);
  43. digitalWrite(pingPin, LOW);
  44. delayMicroseconds(2);
  45. digitalWrite(pingPin, HIGH);
  46. delayMicroseconds(5);
  47. digitalWrite(pingPin, LOW);
  48.  
  49. // The same pin is used to read the signal from the PING))): a HIGH
  50. // pulse whose duration is the time (in microseconds) from the sending
  51. // of the ping to the reception of its echo off of an object.
  52. pinMode(pingPin, INPUT);
  53. duration = pulseIn(pingPin, HIGH);
  54.  
  55. // convert the time into a distance
  56. inches = microsecondsToInches(duration);
  57. cm = microsecondsToCentimeters(duration);
  58.  
  59. Serial.print(inches);
  60. Serial.print("in, ");
  61. Serial.print(cm);
  62. Serial.print("cm");
  63. Serial.println();
  64.  
  65. delay(100);
  66. }
  67.  
  68. long microsecondsToInches(long microseconds)
  69. {
  70. // According to Parallax's datasheet for the PING))), there are
  71. // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  72. // second). This gives the distance travelled by the ping, outbound
  73. // and return, so we divide by 2 to get the distance of the obstacle.
  74. // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  75. return microseconds / 74 / 2;
  76. }
  77.  
  78. long microsecondsToCentimeters(long microseconds)
  79. {
  80. // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  81. // The ping travels out and back, so to find the distance of the
  82. // object we take half of the distance travelled.
  83. return microseconds / 29 / 2;
  84. }
Add Comment
Please, Sign In to add comment