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

robtillaart:

Rewrote the pulseIn to use micros() instead of loop counting.

Be aware that micros() always round its value to a multiple of 4.

Counting the loops in pulsein works quite well, however if there are interrupt handled during this it will definitely give a wrong (too low) reading. This could (partially) explain why you need a smaller divisor in your formulas.
This "trick" is used especially to be able to read pulselengths smaller than 4 micros() precission.

Hope to see the code soon!

Let's put it this way. The pulseIn function is from 460 to 7120 microseconds off because of the way ultrasonic sensors work. Basically, the echo port state takes that long to settle and pulseIn counts that time. So, I think I'll settle for only 4 microseconds off. :wink:

I may release the code in the source both ways so if you want to try it the other way, you can uncomment it.

Tim