I am fairly new to Arduino and I am trying to build a basic ultrasonic 2wd robot. I have installed the NewPing library and hooked it to a breadboard using Pin 7 to the Arduino and a Parallax Ping sensor. My issue is that the sensor is returning zero values when pointed in directions that have no objects near. Near being defined as 6 or more feet away.
If the sensor is pointed at something relatively close, I appear to get accurate readings.
I am assuming that zeros have to do with the MAX_DISTANCE variable being out of range, but I am not sure. If this is the case, then I guess I am surprised that it would not just return a 200 to indicate that the maximum distance has been reached.
Can anyone confirm this or provide any insight as to why the zeros are being returned?
// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------
#include <NewPing.h>
#define TRIGGER_PIN 7 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 7 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
delay(100); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
}
ok, so I did some testing, which I should have done first, and if the max distance is reached, the values drop to zero. I set the max distance to 999 and got accurate readings up to the sensor's max values (about 379). It still seems odd that a zero value would be returned is the max distance is reached, but I'm sure there is a good reason that I am not thinking about.
Smburns25:
ok, so I did some testing, which I should have done first, and if the max distance is reached, the values drop to zero. I set the max distance to 999 and got accurate readings up to the sensor's max values (about 379). It still seems odd that a zero value would be returned is the max distance is reached, but I'm sure there is a good reason that I am not thinking about.
The NewPing library is designed to show a reading of zero if it's out of range. That's how you know it's out of range. If it returned 200, you wouldn't know if there was something at 200cm or nothing. The zero lets you know the correct result. Basically, you're thinking about it wrong. The reason it returns a zero if it's beyond the maximum distance is to let you know it couldn't find anything. Meaning, it's clear.
You also shouldn't set the max value to 999, that will slow down your sketch. Instead, set it to what you really care about reading out to (maybe 379 in your case). But, that's only if you really want to detect something that far away. In many cases, only within a shorter range is required. The sensors are not as reliable at the maximum distance, setting a shorter max distance speeds up your sketch and yields more accurate results, which is why 200cm is used in the examples.
But, if you REALLY want it to return something else, like 379 or 999 or whatever, feel free to change the library's NO_ECHO value to something other than 0. It must be a positive integer. Read the library header file, I've added LOTS of comments that probably would have answered your question in the first place.