From e79b319fd134cc73a486746900f37a370ffcb7e0 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Sat, 8 Nov 2008 21:31:12 +0000 Subject: [PATCH] Waiting for pulse to start (rising or falling edge) to start timing. --- hardware/cores/arduino/wiring_pulse.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/hardware/cores/arduino/wiring_pulse.c b/hardware/cores/arduino/wiring_pulse.c index 01571145b..8f232f1d5 100755 --- a/hardware/cores/arduino/wiring_pulse.c +++ b/hardware/cores/arduino/wiring_pulse.c @@ -44,6 +44,11 @@ unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout) unsigned long numloops = 0; unsigned long maxloops = microsecondsToClockCycles(timeout) / 16; + // wait for any previous pulse to end + while ((*portInputRegister(port) & bit) == stateMask) + if (numloops++ == maxloops) + return 0; + // wait for the pulse to start while ((*portInputRegister(port) & bit) != stateMask) if (numloops++ == maxloops)