CyklKent:
Here is the code that I normally use:
#include <NewPing.h>
#define TRIGGER_PIN 12
#define ECHO_PIN 13
#define MAX_DISTANCE 200 // In Centimeters (200cm = 78.7") It will not keep waiting for a return if past this value
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() {
Serial.begin(9600);
}
void loop() {
float uS = sonar.ping_median(5);
Serial.print(uS / US_ROUNDTRIP_IN);
Serial.print("a");
}
I was hoping to get the picture of your testing jig in the test environment to look for possible echo sources.
But, looking at just your sketch I can see a problem. Your sketch is sending a ping on top of an old ping, with no pause between. This won't give reliable results. What you'll get is possible echos. You MUST wait AT LEAST 29ms between pings. The reason you shouldn't go faster has to do with the sensor hardware and the speed of sound. The sensors can sense up to around 500cm away (1000cm round-trip). Speed of sound is around 29uS per cm. That works out to 29ms. If you don't wait AT LEAST 29ms, you could get an ping echo from a previous ping giving you all kinds of strange results.
While the ping_median() method adds a delay between each ping, it doesn't add a delay before or at the end. So, the first ping is basically on top of the last ping. It doesn't add a delay at the end of the ping_median() on purpose, so you can add whatever delay you want, or none at all if it's a one-time only ping.
So, after the sonar.ping_median(5), you need to add a line with something like "delay(29);"
Also, you shouldn't use pin 13 if you're using an Arduino as there's an LED on that pin. Change that to any other pin to avoid possible conflict. I stopped using pin 13 in my sample sketches due to a couple users having problems with an LED conflict. Best to avoid that.
If you still have a problem, I would lengthen the delay between pings to something much larger, say 100ms. You need to change the delay(29) to delay(100) as well as edit the NewPing.h file and change the PING_MEDIAN_DELAY from 29000 to 100000. While a ping delay of 29ms should be long enough, if the sensor is too sensitive (say it can sense up to 600cm away, it would need at least a 600229=34800uS [35ms] delay between pings).
Tim