Skip to content

Commit 4dad135

Browse files
committed
pulseIn() now times out while measuring the pulse, not just while waiting for it to start.
1 parent 012b4b0 commit 4dad135

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

hardware/arduino/cores/arduino/wiring_pulse.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,15 @@ unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
5555
return 0;
5656

5757
// wait for the pulse to stop
58-
while ((*portInputRegister(port) & bit) == stateMask)
58+
while ((*portInputRegister(port) & bit) == stateMask) {
59+
if (numloops++ == maxloops)
60+
return 0;
5961
width++;
62+
}
6063

6164
// convert the reading to microseconds. The loop has been determined
62-
// to be 10 clock cycles long and have about 16 clocks between the edge
65+
// to be 20 clock cycles long and have about 16 clocks between the edge
6366
// and the start of the loop. There will be some error introduced by
6467
// the interrupt handlers.
65-
return clockCyclesToMicroseconds(width * 10 + 16);
68+
return clockCyclesToMicroseconds(width * 21 + 16);
6669
}

0 commit comments

Comments
 (0)