pulseIn fix (prior to arduino 13)

I was tickled to see in arduino 13 release notes that a pulseIn had a problem prior to arduino 13. That meant I wasn’t crazy.

From the release notes:
“Modifying pulseIn() to wait for a transition to start timing (i.e. ignoring any pulse that had already started when the function was called).”

I have been having trouble with v13 but this work-around is performing well on v10.

Thanks for all the work on arduino!


/* fix arduino bug that gets wrong PW */
int myPulseIn(int pin){
long timeout=1000;
long start = millis();
long pulse_start;

// wait for low
while(millis() < start + timeout && digitalRead(pin) == HIGH){
return pulseIn(pin, HIGH);