if (newtime > (savedtime +( savedtime / 2))
Quote if (newtime > (savedtime +( savedtime / 2))Maybe also replace this withif (newtime > (savedtime + (savedtime >> 1)))I'm not sure if the compiler is smart enough to detect division by a power of two, but I never like to take the chance. If for some reason it's not, division takes a whole lot longer than bit-shifting, which takes a single instruction cycle to perform on a byte.Also, if possible, work with unsigned integers (or even unsigned chars) rather than longs (they're half the size and hence the mathematical operations involving them are much faster). For example, you are expecting your pulses to be only a few hundred microseconds, so you should be able to safely cast the result of the pulseIn() call to an unsigned int.- Ben
Make sure the digital input pin is HIGH before calling pulseIn(pin, LOW).Otherwise it may not measure the pulse width correctly.
The pulseIn routine does that for you.It checks and waits for an already started pulse to finish before starting to measure.The source code is in wiring_pulse.c if you want to see how it does it.
What happens if you don't specify the timeout value?
Please enter a valid email to subscribe
We need to confirm your email address.
To complete the subscription, please click the link in the
email we just sent you.
Thank you for subscribing!
via Egeo 16