This is admittedly my first Arduino expedition. I'm trying to use the HC-SR04 sensor to feed a Max/MSP path for live processing of media. Yesterday, using the code below, the serial monitor was accurately reporting distance. I then tried parsing those messages in Max (unsuccessfully) and started to work on customizing the Arduino code to better suit my eventual needs. At some point, the sensor started returning negative, random values and stopped responding to changes in the distance of the objects placed in front of it. I've tried swapping pins (code and hardware), going back to the original code, using alternate libraries (ultrasonic.h), rebuilding from scratch with new wires - still it's echoing seemingly random negative distance numbers. Is it possible I ruined the sensor somehow? Can any of you wonderful folks help?? I'm using an UNO Rev 3 connected to a Macbook pro (10.6.8) and using Arduino 1.0 for coding.
Many thanks in advance!
/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
*/
The example code for pulsein and ultrasonic distance measurements use either long or unsigned long. It sounds like you're getting an overflow using int. Change duration to be long.
After uploading, the sensor is still unresponsive to variance in distance from objects, but now the reported distance it's spitting out "3353 cm" constantly on the serial monitor.
as an addendum, I tried powering the sensor from the 3.3v pin and the number oddly dropped to the mid 40s. Still not responding to proximity to objects...
Yes, today I tried both the newPing and ultrasonic.h libraries which both exhibited the same basic results - no response to proximity changes and echoes of static numbers like 3335,3345,3355 etc. I have noticed that the echoes change significantly when supply voltage is lowered or raised. I'm wondering if there's a power issue with what's being supplied by the USB bus. Is it possible I could've somehow damaged the sensor and this is symptomatic of a bad circuit? Should I check the in and out voltages of the sensors with a voltmeter?
I'm going to try rebuilding this whole setup tomorrow using a PC to see if there's any change. It's just weird because it was totally working at one point with the sample code above...