Getting familiar with the sensor
It's now time to write the code for the sensor. Let's output the values of the sensor to Arduinos Serial Monitor and play around with the sensor a little. We are going to use the ping_cm()
function from the NewPing
library. Other functions of the library are listed at http://playground.arduino.cc/Code/NewPing.
According to the web page the function is explained as follows:
sonar.ping_cm()
; Sends a ping, returns the distance in centimeters or 0 (zero) if no ping echo within set distance limit. There are other functions (also called Methods in some cases) that return the distance in inches, or the time it took for the eco to return, mentioned on the webpage.
Let's get coding for our sensor!
#include <NewPing.h> NewPing sonar(8, 9); void setup() { Serial.begin(115200); } void loop() { delay(50); int dist = sonar.ping_cm(); Serial.print("Distance: "); Serial.print(dist); &...