Hi,
I've been using the pulseIn() function on a mega 2560 to measure the width of a pulse returned from an EZ4 ultrasonic rangefinder.
This is what the reference page says about the syntax....
Syntax
pulseIn(pin, value, timeout)
Parameterspin: the number of the pin on which you want to read the pulse. (int)
value: type of pulse to read: either HIGH or LOW. (int)
timeout (optional): the number of microseconds to wait for the pulse to start; default is one second (unsigned long)
My question is this, why does the function always take a time equal to timeout to return a value? Shouldn't timeout be the maximum?
Here's a code snippet illustrating what I'm talking about......
unsigned long A=millis();
pulse = pulseIn(_ultraPin, HIGH,PULSE_IN_TIMEOUT);
unsigned long B=millis();
Serial.print("Duration of pulseIn() <");Serial.print(B-A);Serial.print(">ms. Pulse width <");Serial.print(pulse);Serial.print(">us. Distance <");Serial.print(pulse/PULSE_SIZE_CM);Serial.println(">cm.");
If PULSE_IN_TIMEOUT is set to a hundred thousand (microsconds), then the code above indicates that pulseIn() always takes 97 ms. If I up the value to two hundred thousand, then the duration increases to 185ms, and if I drop it to fifty thousand then the duration drops to about 48ms. Note that in all these scenarios, the pulseIn() function always returns the correct value, it just always takes too long! I would expect pulseIn to take the length of time neccessary to detect and measure a pulse, so a short pulse would be measured quickly, and a long pulse would take a bit longer. What actually happens is that it always takes exactly the same time!
Have I just mis-understood what the function is supposed to do? or is something going wrong?
thanks