Skip to content

Commit 438d59f

Browse files
committed
pulseInLong: fix incorrect timeout handling
1 parent 5275d98 commit 438d59f

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

avr/cores/arduino/wiring_pulse.c

+4-7
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,22 @@ unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout)
6969
uint8_t port = digitalPinToPort(pin);
7070
uint8_t stateMask = (state ? bit : 0);
7171

72-
// convert the timeout from microseconds to a number of times through
73-
// the initial loop; it takes 16 clock cycles per iteration.
74-
unsigned long numloops = 0;
75-
unsigned long maxloops = microsecondsToClockCycles(timeout);
72+
unsigned long maxMicros = micros() + timeout;
7673

7774
// wait for any previous pulse to end
7875
while ((*portInputRegister(port) & bit) == stateMask)
79-
if (numloops++ == maxloops)
76+
if (micros() > maxMicros)
8077
return 0;
8178

8279
// wait for the pulse to start
8380
while ((*portInputRegister(port) & bit) != stateMask)
84-
if (numloops++ == maxloops)
81+
if (micros() > maxMicros)
8582
return 0;
8683

8784
unsigned long start = micros();
8885
// wait for the pulse to stop
8986
while ((*portInputRegister(port) & bit) == stateMask) {
90-
if (numloops++ == maxloops)
87+
if (micros() > maxMicros)
9188
return 0;
9289
}
9390
return micros() - start;

0 commit comments

Comments
 (0)