Clean up two-second interval check

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.
This commit is contained in:
Matthijs Kooijman 2015-07-24 17:40:38 +02:00
parent 04905bc5cd
commit f1b79028ea

View File

@ -117,15 +117,11 @@ boolean DHT::read(bool force) {
// Check if sensor was read less than two seconds ago and return early // Check if sensor was read less than two seconds ago and return early
// to use last reading. // to use last reading.
uint32_t currenttime = millis(); uint32_t currenttime = millis();
if (currenttime < _lastreadtime) {
// ie there was a rollover
_lastreadtime = 0;
}
if (!force && !_firstreading && ((currenttime - _lastreadtime) < 2000)) { if (!force && !_firstreading && ((currenttime - _lastreadtime) < 2000)) {
return _lastresult; // return last correct measurement return _lastresult; // return last correct measurement
} }
_firstreading = false; _firstreading = false;
_lastreadtime = millis(); _lastreadtime = currenttime;
// Reset 40 bits of received data to zero. // Reset 40 bits of received data to zero.
data[0] = data[1] = data[2] = data[3] = data[4] = 0; data[0] = data[1] = data[2] = data[3] = data[4] = 0;