Permalink
Browse files

pulseIn() now times out while measuring the pulse, not just while wai…

…ting for it to start.
  • Loading branch information...
1 parent 012b4b0 commit 4dad13532fbd9fcc14acd157d3fc20e295c38101 @damellis damellis committed Nov 23, 2010
Showing with 6 additions and 3 deletions.
  1. +6 −3 hardware/arduino/cores/arduino/wiring_pulse.c
View
9 hardware/arduino/cores/arduino/wiring_pulse.c
@@ -55,12 +55,15 @@ unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
return 0;
// wait for the pulse to stop
- while ((*portInputRegister(port) & bit) == stateMask)
+ while ((*portInputRegister(port) & bit) == stateMask) {
+ if (numloops++ == maxloops)
+ return 0;
width++;
+ }
// convert the reading to microseconds. The loop has been determined
- // to be 10 clock cycles long and have about 16 clocks between the edge
+ // to be 20 clock cycles long and have about 16 clocks between the edge
// and the start of the loop. There will be some error introduced by
// the interrupt handlers.
- return clockCyclesToMicroseconds(width * 10 + 16);
+ return clockCyclesToMicroseconds(width * 21 + 16);
}

0 comments on commit 4dad135

Please sign in to comment.