-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdistance_reader.ino
73 lines (57 loc) · 2.3 KB
/
distance_reader.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
Ping))) Sensor
This sketch reads a PING))) ultrasonic rangefinder and returns the distance
to the closest object in range. To do this, it sends a pulse to the sensor to
initiate a reading, then listens for a pulse to return. The length of the
returning pulse is proportional to the distance of the object from the sensor.
The circuit:
- +V connection of the PING))) attached to +5V
- GND connection of the PING))) attached to ground
- SIG connection of the PING))) attached to digital pin 7
created 3 Nov 2008
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Ping
*/
// this constant won't change. It's the pin number of the sensor's output:
const int pingPin = 7;
const int led = 2; // pin number of the LED
void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize LED
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}
void loop() {
// establish variables for duration of the ping, and the distance result in meters:
float duration, meters;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH pulse
// whose duration is the time (in microseconds) from the sending of the ping
// to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// Convert the time into a distance
meters = microsecondsToMeters(duration);
Serial.print(meters, 3);
Serial.println();
delay(100);
if (meters <= 2) digitalWrite(led, HIGH); // turn on LED if object is within 2 meters
else digitalWrite(led, LOW); // turn off LED if no object is within 2 meters
}
float microsecondsToMeters(float microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the object we
// take half of the distance travelled.
return microseconds / 29 / 2 / 100;
}