I wanted to add that I have a HC-SR04 that doesn't work with NewPing 1.2, 1.3, or the 1.4 pre-release, using either the Simple NewPing Example Sketch or the 15-sensor example, modified for 1 HC-SR04. It works fine with the example code at http://goo.gl/kJ8Gl though.
How can I help debug the library for the HC-SR04?
EDIT: Forgot to add that I'm using an Arduino Uno r3 with the v1.0 Arduino IDE.
EDIT2: I get 0cm like everyone else with an HC-SR04 reported. I even tried getting the raw timing data instead, in case it was an obscure bug in the distance conversion. Everything I get is 0.
EDIT3: In case it's helpful, I note that I can hear the HC-SR04 sending out its sonar pings when I try NewPing. So I would guess that the bug doesn't involve actually sending out the pings.
I have (4) HC-SR04 sensors and they all work just fine with the Arduino Uno R3 using every library version of NewPing as well as every example sketch. I've also run with Arduino 0023, v1.0 and have just switched to v1.0.1 all of which work. So, I believe it's unfair to say "I get 0cm
like everyone else with an HC-SR04 reported". There's only a couple people that have reported a problem, it's running just fine for me with 4 different sensors, so not everyone is having a problem.
There's differences between my example sketches and the sketch at the link you provided. For example, it's only doing 2 pings a second while my examples are typically doing 20 pings a second. So, maybe your sensor can't ping that frequently. To diagnose this, I've modified my example sketch to duplicate what that other example is doing.
#include <NewPing.h>
#define trigPin 12
#define echoPin 11
NewPing sonar(trigPin, echoPin);
void setup() {
Serial.begin(9600);
}
void loop() {
int distance = sonar.ping_cm();
Serial.print(distance);
Serial.println("cm");
delay(500);
}
If this still doesn't work, it could be the VERY long trigger delay in the example you provided. It's waiting 1000 microseconds when the sensor specs state it should only require 10 microseconds. If your sensor is way out of spec, maybe it needs longer than 10 microseconds. To change this in the NewPing library, look for the "delayMicroseconds(10)" in the NewPing.cpp file (line 48 in the 1.4 pre-release) and change it from 10 microseconds to 1000 microseconds.
If it still doesn't work, try changing the MAX_SENSOR_DELAY in the NewPing.h file from 18000 to 180000 (add a zero at the end). This will give your sensor more time to finish the ping before waiting for the echo. The longest I've ever measured is around a 17000 microsecond delay, but maybe your sensor requires longer.
If it's still not working for you, you could send me your sensor. If I had the sensor, I could hopefully duplicate it and see what exactly is happening. Of course, I'd send it back when I was done with the diagnosis.
Let me know what you find or if you want to send me the sensor for diagnosis.
Tim