Great work on the library! I thought I might use this thread for a bit of troubleshooting.
My HC-SR04 is doing some very bizarre things and I'm lost. When using newPing and it's included newpingexample sketch, the sensor returns "0 cm" and there is no reaction to changes in proximity to objects. I tried using the simplest code I could find which doesn't require libraries (pasted below), but the same thing happens, this time reporting strange negative distances (-39 through - 45). I've found that changing the voltage supply effects the numbers, but only once did I get it to correctly report distance and very briefly. Is it possible the sensor has been damaged in some way? I'm using an Uno Rev 3 and a macbook pro (10.6.8 ) for config.
/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
*/
int echoPin = 13;
int trigPin = 12;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
int duration, cm;
digitalWrite(trigPin, LOW);
delayMicroseconds(20);
digitalWrite(trigPin, HIGH);
delayMicroseconds(20);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
cm = duration / 29 / 2;
/* Sound velocity=29cm/microsec,
then divide by 2 because of the return time */
Serial.print(cm);
Serial.println(" cm");
delay(500);
}