mirror of
https://github.com/adafruit/DHT-sensor-library.git
synced 2023-10-23 22:20:38 +03:00
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:
parent
04905bc5cd
commit
f1b79028ea
6
DHT.cpp
6
DHT.cpp
@ -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;
|
||||||
|
Loading…
Reference in New Issue
Block a user