From ef4eb0cf6a2afd7dfc3b67561c8523ac8d4c6205 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 2 Mar 2015 13:47:16 +0100 Subject: pulseIn: be consistent with standard API return 0 if timeout has been reached --- cores/arduino/wiring_pulse.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'cores') 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 -- cgit v1.2.3-18-g5258