You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi Jeremy,
Many thanks for producing this library!
Found a bug, here is the explanation and the fix.
echoInMicroseconds() function has while loops that if sensor is failing / not present / disconnected will never end.
original code within echoInMicroseconds()
while(digitalRead(_echo) == LOW);
int pulseStart = micros();
while(digitalRead(_echo) == HIGH);
fixed code within echoInMicroseconds()
// sounds go at 350meters/sec, HCSR04 reach is 4mt, to be safe 10mt, at sound speed takes 28 millis
const long timeout=100L; // more than safe in normal conditions.
long started = millis();
boolean timedout = false;
while(digitalRead(_echo) == LOW && !(timedout=(millis()-started > timeout)));
if (timedout) return -1; // negative distance should be treated as error condition.
int pulseStart = micros();
started = millis();
while(digitalRead(_echo) == HIGH && !(timedout=(millis()-started > timeout)));
if (timedout) return -1; // negative distance should be treated as error condition.
That's all.
Regards
The text was updated successfully, but these errors were encountered:
Hi Jeremy,
Many thanks for producing this library!
Found a bug, here is the explanation and the fix.
echoInMicroseconds() function has while loops that if sensor is failing / not present / disconnected will never end.
original code within echoInMicroseconds()
fixed code within echoInMicroseconds()
That's all.
Regards
The text was updated successfully, but these errors were encountered: