source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/examples/06.Sensors/Ping/Ping.ino @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

File size: 2.5 KB
Line 
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:
27const int pingPin = 7;
28
29void setup() {
30  // initialize serial communication:
31  Serial.begin(9600);
32}
33
34void loop() {
35  // establish variables for duration of the ping,
36  // and the distance result in inches and centimeters:
37  long duration, inches, cm;
38
39  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
40  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
41  pinMode(pingPin, OUTPUT);
42  digitalWrite(pingPin, LOW);
43  delayMicroseconds(2);
44  digitalWrite(pingPin, HIGH);
45  delayMicroseconds(5);
46  digitalWrite(pingPin, LOW);
47
48  // The same pin is used to read the signal from the PING))): a HIGH
49  // pulse whose duration is the time (in microseconds) from the sending
50  // of the ping to the reception of its echo off of an object.
51  pinMode(pingPin, INPUT);
52  duration = pulseIn(pingPin, HIGH);
53
54  // convert the time into a distance
55  inches = microsecondsToInches(duration);
56  cm = microsecondsToCentimeters(duration);
57
58  Serial.print(inches);
59  Serial.print("in, ");
60  Serial.print(cm);
61  Serial.print("cm");
62  Serial.println();
63
64  delay(100);
65}
66
67long microsecondsToInches(long microseconds) {
68  // According to Parallax's datasheet for the PING))), there are
69  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
70  // second).  This gives the distance travelled by the ping, outbound
71  // and return, so we divide by 2 to get the distance of the obstacle.
72  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
73  return microseconds / 74 / 2;
74}
75
76long microsecondsToCentimeters(long microseconds) {
77  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
78  // The ping travels out and back, so to find the distance of the
79  // object we take half of the distance travelled.
80  return microseconds / 29 / 2;
81}
Note: See TracBrowser for help on using the repository browser.