diff options
author | Martino Facchin <m.facchin@arduino.cc> | 2015-03-02 13:47:16 +0100 |
---|---|---|
committer | Martino Facchin <m.facchin@arduino.cc> | 2015-05-29 15:01:38 +0200 |
commit | ef4eb0cf6a2afd7dfc3b67561c8523ac8d4c6205 (patch) | |
tree | f44fc0c1e1e9fe3c20205d324279fbca2fd458e4 | |
parent | 9e7c7c4ddc9919ad21c95397cdf4900ba25fa090 (diff) |
pulseIn: be consistent with standard API
return 0 if timeout has been reached
-rw-r--r-- | cores/arduino/wiring_pulse.c | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/cores/arduino/wiring_pulse.c b/cores/arduino/wiring_pulse.c index 49fa38d..4da446c 100644 --- a/cores/arduino/wiring_pulse.c +++ b/cores/arduino/wiring_pulse.c @@ -48,7 +48,12 @@ unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout) unsigned long maxloops = microsecondsToClockCycles(timeout)/16; width = countPulseASM(portInputRegister(port), bit, stateMask, maxloops); - return clockCyclesToMicroseconds(width * 16 + 16); + + //prevent clockCyclesToMicroseconds to return bogus values if countPulseASM timed out + if (width) + return clockCyclesToMicroseconds(width * 16 + 16); + else + return 0; } /* Measures the length (in microseconds) of a pulse on the pin; state is HIGH |