hi, I am using the NewPing v1.8 library with an arduino and hc sr04.
An odd thing that i found was if I limit the sensor to say 50cm, and the object was at say 70cm. the sonar.ping_cm() function(or any other function) would return 0.
Now this to my arduino means that the object is at 0 cm.
So, this becomes an issue as there are two circumstances where in the object can be at 0cm. When it is actually at 0cm and when it is at >50cm.
Is their any way to change that default 0 value to say the max value ie 50cm.
thnx
When you place the sensor right up against a surface you get zero not because the distance is zero but because the sensor is blocked and no echo is received. You can tell because the time it takes to get the reading is the timeout time, not the shorter time of a valid reading:
Limit set to 200 cm (2 meters).
Pointed at ceiling:
Ping: 170 cm, 10192 Microseconds
Pointed at bookcase across the room:
Ping: 0 cm in 11928 Microseconds (timeout)
Pointed at nearby hard surface:
Ping: 4 cm in 728 Microseconds
Against a hard surface:
Ping: 0 cm in 11924 Microseconds (timeout)
My HC-SR04 sensor seems to get erratic at distances below about 2 cm. I would not expect accurate results any closer than that. If you really need to distinguish between "zero because my sensor is up against something" and "zero because I didn't receive an echo in time" you could add a separate short-range IR proximity detector.