NewPing Library: HC-SR04, SRF05, SRF06, DYP-ME007, Parallax PING))) - v1.7

robtillaart:
Hi Teckel,

did investigate the duration2cm() function here - http://arduino.cc/forum/index.php/topic,165860.0.html -
might be useful in your library.

I tried something similar in the past, but honestly it's not worth the extra cycles adding this type of code adds. Basically, it's pointless to try to make the math more accurate than the sensor is. Ultrasonic sensors are not very accurate anyway. Also, there's actually an argument as to the speed of sound (everyone likes to use a different number). This is why I settled on an integer and the value that I default to. It provides very accurate distance results in typical indoor temperatures and does so very fast using very little program space.

Instead of getting into this battle, I avoid it. Just use the ping() method which returns a ping time. Then, use whatever math you want (based on temperature or whatever). If you want to use a fraction and want to use a different method to calculate this (like the above linked example) that's fine also.

Keep in mind that NewPing is an ultrasonic ping library, not a floating point, math, or temperature library. I'm sure you can find or develop a library that allows you to divide a floating point number without using floating point math. This would be then just included in your sketch along with NewPing to calculate a distance at whatever accuracy level you desired. But keep in mind that the sensor isn't that accurate anyway (only about 1cm). So all this effort may be wasted anyway.

Tim