Skip to content

Commit

Permalink
Clean up two-second interval check
Browse files Browse the repository at this point in the history
There was some handling of millis() overflow that is not needed.
Standard unsigned subtraction and wraparound already works as expected,
so this extra check can be removed (it even hurts, since it introduces 2
seconds after a wraparound where no new data will be read, even if it
could otherwise happen).

Also, this prevents calling millis() a second time, since its value is
already known.
  • Loading branch information
matthijskooijman committed Jul 24, 2015
1 parent 04905bc commit f1b7902
Showing 1 changed file with 1 addition and 5 deletions.
6 changes: 1 addition & 5 deletions DHT.cpp
Expand Up @@ -117,15 +117,11 @@ boolean DHT::read(bool force) {
// Check if sensor was read less than two seconds ago and return early
// to use last reading.
uint32_t currenttime = millis();
if (currenttime < _lastreadtime) {
// ie there was a rollover
_lastreadtime = 0;
}
if (!force && !_firstreading && ((currenttime - _lastreadtime) < 2000)) {
return _lastresult; // return last correct measurement
}
_firstreading = false;
_lastreadtime = millis();
_lastreadtime = currenttime;

// Reset 40 bits of received data to zero.
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
Expand Down

0 comments on commit f1b7902

Please sign in to comment.