From 761f4f4fb94c01afc283d0ffbfbfdeed5bb18a44 Mon Sep 17 00:00:00 2001 From: adams13x13 Date: Sat, 2 Sep 2017 20:15:19 +0200 Subject: [PATCH] Fixes #81 "signal already there" != "timeout" If expectPulse(LOW) sees the port is already LOW (result == 0), this is not timeout! --- DHT.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/DHT.cpp b/DHT.cpp index 86ad91c..28f8a13 100644 --- a/DHT.cpp +++ b/DHT.cpp @@ -7,6 +7,7 @@ written by Adafruit Industries #include "DHT.h" #define MIN_INTERVAL 2000 +#define TIMEOUT -1 DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) { _pin = pin; @@ -161,12 +162,12 @@ boolean DHT::read(bool force) { // First expect a low signal for ~80 microseconds followed by a high signal // for ~80 microseconds again. - if (expectPulse(LOW) == 0) { + if (expectPulse(LOW) == TIMEOUT) { DEBUG_PRINTLN(F("Timeout waiting for start signal low pulse.")); _lastresult = false; return _lastresult; } - if (expectPulse(HIGH) == 0) { + if (expectPulse(HIGH) == TIMEOUT) { DEBUG_PRINTLN(F("Timeout waiting for start signal high pulse.")); _lastresult = false; return _lastresult; @@ -191,7 +192,7 @@ boolean DHT::read(bool force) { for (int i=0; i<40; ++i) { uint32_t lowCycles = cycles[2*i]; uint32_t highCycles = cycles[2*i+1]; - if ((lowCycles == 0) || (highCycles == 0)) { + if ((lowCycles == TIMEOUT) || (highCycles == TIMEOUT)) { DEBUG_PRINTLN(F("Timeout waiting for pulse.")); _lastresult = false; return _lastresult; @@ -242,7 +243,7 @@ uint32_t DHT::expectPulse(bool level) { uint8_t portState = level ? _bit : 0; while ((*portInputRegister(_port) & _bit) == portState) { if (count++ >= _maxcycles) { - return 0; // Exceeded timeout, fail. + return TIMEOUT; // Exceeded timeout, fail. } } // Otherwise fall back to using digitalRead (this seems to be necessary on ESP8266 @@ -250,7 +251,7 @@ uint32_t DHT::expectPulse(bool level) { #else while (digitalRead(_pin) == level) { if (count++ >= _maxcycles) { - return 0; // Exceeded timeout, fail. + return TIMEOUT; // Exceeded timeout, fail. } } #endif