From f1b79028ea5f3c11a8ae0d2c06452de1d293b559 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Fri, 24 Jul 2015 17:40:38 +0200 Subject: [PATCH] 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. --- DHT.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/DHT.cpp b/DHT.cpp index 659dd97..6bc87c9 100644 --- a/DHT.cpp +++ b/DHT.cpp @@ -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;