Skip to content

Commit 761f4f4

Browse files
authored
Fixes #81 "signal already there" != "timeout"
If expectPulse(LOW) sees the port is already LOW (result == 0), this is not timeout!
1 parent c978977 commit 761f4f4

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

DHT.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ written by Adafruit Industries
77
#include "DHT.h"
88

99
#define MIN_INTERVAL 2000
10+
#define TIMEOUT -1
1011

1112
DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
1213
_pin = pin;
@@ -161,12 +162,12 @@ boolean DHT::read(bool force) {
161162

162163
// First expect a low signal for ~80 microseconds followed by a high signal
163164
// for ~80 microseconds again.
164-
if (expectPulse(LOW) == 0) {
165+
if (expectPulse(LOW) == TIMEOUT) {
165166
DEBUG_PRINTLN(F("Timeout waiting for start signal low pulse."));
166167
_lastresult = false;
167168
return _lastresult;
168169
}
169-
if (expectPulse(HIGH) == 0) {
170+
if (expectPulse(HIGH) == TIMEOUT) {
170171
DEBUG_PRINTLN(F("Timeout waiting for start signal high pulse."));
171172
_lastresult = false;
172173
return _lastresult;
@@ -191,7 +192,7 @@ boolean DHT::read(bool force) {
191192
for (int i=0; i<40; ++i) {
192193
uint32_t lowCycles = cycles[2*i];
193194
uint32_t highCycles = cycles[2*i+1];
194-
if ((lowCycles == 0) || (highCycles == 0)) {
195+
if ((lowCycles == TIMEOUT) || (highCycles == TIMEOUT)) {
195196
DEBUG_PRINTLN(F("Timeout waiting for pulse."));
196197
_lastresult = false;
197198
return _lastresult;
@@ -242,15 +243,15 @@ uint32_t DHT::expectPulse(bool level) {
242243
uint8_t portState = level ? _bit : 0;
243244
while ((*portInputRegister(_port) & _bit) == portState) {
244245
if (count++ >= _maxcycles) {
245-
return 0; // Exceeded timeout, fail.
246+
return TIMEOUT; // Exceeded timeout, fail.
246247
}
247248
}
248249
// Otherwise fall back to using digitalRead (this seems to be necessary on ESP8266
249250
// right now, perhaps bugs in direct port access functions?).
250251
#else
251252
while (digitalRead(_pin) == level) {
252253
if (count++ >= _maxcycles) {
253-
return 0; // Exceeded timeout, fail.
254+
return TIMEOUT; // Exceeded timeout, fail.
254255
}
255256
}
256257
#endif

0 commit comments

Comments
 (0)