|
| 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 | + modified 1 Mar 2017 for use with sduino |
| 21 | + by Michael Mayer |
| 22 | +
|
| 23 | + This example code is in the public domain. |
| 24 | +
|
| 25 | + */ |
| 26 | + |
| 27 | +#include <Arduino.h> |
| 28 | + |
| 29 | +// this constant won't change. It's the pin number |
| 30 | +// of the sensor's output: |
| 31 | +const int pingPin = 7; |
| 32 | + |
| 33 | +long microsecondsToInches(long microseconds); |
| 34 | +long microsecondsToCentimeters(long microseconds); |
| 35 | + |
| 36 | +void setup() { |
| 37 | + // initialize serial communication: |
| 38 | + Serial_begin(9600); |
| 39 | +} |
| 40 | + |
| 41 | +void loop() { |
| 42 | + // establish variables for duration of the ping, |
| 43 | + // and the distance result in inches and centimeters: |
| 44 | + long duration, inches, cm; |
| 45 | + |
| 46 | + // The PING))) is triggered by a HIGH pulse of 2 or more microseconds. |
| 47 | + // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: |
| 48 | + pinMode(pingPin, OUTPUT); |
| 49 | + digitalWrite(pingPin, LOW); |
| 50 | + delayMicroseconds(2); |
| 51 | + digitalWrite(pingPin, HIGH); |
| 52 | + delayMicroseconds(5); |
| 53 | + digitalWrite(pingPin, LOW); |
| 54 | + |
| 55 | + // The same pin is used to read the signal from the PING))): a HIGH |
| 56 | + // pulse whose duration is the time (in microseconds) from the sending |
| 57 | + // of the ping to the reception of its echo off of an object. |
| 58 | + pinMode(pingPin, INPUT); |
| 59 | + duration = pulseInLong(pingPin, HIGH, 200000L); |
| 60 | + |
| 61 | + // convert the time into a distance |
| 62 | + inches = microsecondsToInches(duration); |
| 63 | + cm = microsecondsToCentimeters(duration); |
| 64 | + |
| 65 | + Serial_print_u(inches); |
| 66 | + Serial_print_s("in, "); |
| 67 | + Serial_print_u(cm); |
| 68 | + Serial_print_s("cm"); |
| 69 | + Serial_println(); |
| 70 | + |
| 71 | + delay(100); |
| 72 | +} |
| 73 | + |
| 74 | +long microsecondsToInches(long microseconds) { |
| 75 | + // According to Parallax's datasheet for the PING))), there are |
| 76 | + // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per |
| 77 | + // second). This gives the distance travelled by the ping, outbound |
| 78 | + // and return, so we divide by 2 to get the distance of the obstacle. |
| 79 | + // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf |
| 80 | + return microseconds / 74 / 2; |
| 81 | +} |
| 82 | + |
| 83 | +long microsecondsToCentimeters(long microseconds) { |
| 84 | + // The speed of sound is 340 m/s or 29 microseconds per centimeter. |
| 85 | + // The ping travels out and back, so to find the distance of the |
| 86 | + // object we take half of the distance travelled. |
| 87 | + return microseconds / 29 / 2; |
| 88 | +} |
0 commit comments