Skip to content

Commit aa688fb

Browse files
committed
Fix pulseIn() bug when pin state is HIGH
1 parent 9ea3c92 commit aa688fb

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

cores/arduino/wiring_pulse.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -243,21 +243,23 @@ unsigned long pulseIn(PinName pin, PinStatus state, unsigned long timeout)
243243

244244
unsigned long startMicros = micros();
245245

246+
uint32_t target = state ? mask : 0;
247+
246248
// wait for any previous pulse to end
247-
while ((*reg_in & mask) == state) {
249+
while ((*reg_in & mask) == target) {
248250
if (micros() - startMicros > timeout)
249251
return 0;
250252
}
251253

252254
// wait for the pulse to start
253-
while ((*reg_in & mask) != state) {
255+
while ((*reg_in & mask) != target) {
254256
if (micros() - startMicros > timeout)
255257
return 0;
256258
}
257259

258260
unsigned long start = micros();
259261
// wait for the pulse to stop
260-
while ((*reg_in & mask) == state) {
262+
while ((*reg_in & mask) == target) {
261263
if (micros() - startMicros > timeout)
262264
return 0;
263265
}

0 commit comments

Comments
 (0)