EasyManua.ls Logo

Arduino Pro Mini - Page 98

Arduino Pro Mini
311 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
Figure 15—PING))) basic circuit
digitalWrite(PING_SENSOR_IO_PIN, LOW);
15
-
pinMode(PING_SENSOR_IO_PIN, INPUT);
-
const unsigned long duration = pulseIn(PING_SENSOR_IO_PIN, HIGH);
-
if (duration == 0) {
-
Serial.println("Warning: We did not get a pulse from sensor.");
20
} else {
-
Serial.print("Distance to nearest object: ");
-
Serial.print(microseconds_to_cm(duration));
-
Serial.println(" cm");
-
}
25
-
delay(100);
-
}
-
-
unsigned long microseconds_to_cm(const unsigned long microseconds) {
30
return microseconds / 29 / 2;
-
}
-
First we define a constant for the IO pin the PING))) sensor is connected to.
If you want to connect your sensor to another digital IO pin, you have to
change the program’s first line. In the
setup
method, we set the serial port’s
baud rate to 9600, because we’d like to see some sensor data on the serial
monitor.
Chapter 5. Sensing the World Around Us • 80
report erratum • discuss
www.it-ebooks.info

Table of Contents