Skip to content

Commit dff6509

Browse files
Merge pull request #82 from adams13x13/patch-1
Fixes #81 Distinguish "signal already there" from "timeout"
2 parents 73409c1 + 761f4f4 commit dff6509

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;
@@ -157,12 +158,12 @@ boolean DHT::read(bool force) {
157158

158159
// First expect a low signal for ~80 microseconds followed by a high signal
159160
// for ~80 microseconds again.
160-
if (expectPulse(LOW) == 0) {
161+
if (expectPulse(LOW) == TIMEOUT) {
161162
DEBUG_PRINTLN(F("Timeout waiting for start signal low pulse."));
162163
_lastresult = false;
163164
return _lastresult;
164165
}
165-
if (expectPulse(HIGH) == 0) {
166+
if (expectPulse(HIGH) == TIMEOUT) {
166167
DEBUG_PRINTLN(F("Timeout waiting for start signal high pulse."));
167168
_lastresult = false;
168169
return _lastresult;
@@ -187,7 +188,7 @@ boolean DHT::read(bool force) {
187188
for (int i=0; i<40; ++i) {
188189
uint32_t lowCycles = cycles[2*i];
189190
uint32_t highCycles = cycles[2*i+1];
190-
if ((lowCycles == 0) || (highCycles == 0)) {
191+
if ((lowCycles == TIMEOUT) || (highCycles == TIMEOUT)) {
191192
DEBUG_PRINTLN(F("Timeout waiting for pulse."));
192193
_lastresult = false;
193194
return _lastresult;
@@ -238,15 +239,15 @@ uint32_t DHT::expectPulse(bool level) {
238239
uint8_t portState = level ? _bit : 0;
239240
while ((*portInputRegister(_port) & _bit) == portState) {
240241
if (count++ >= _maxcycles) {
241-
return 0; // Exceeded timeout, fail.
242+
return TIMEOUT; // Exceeded timeout, fail.
242243
}
243244
}
244245
// Otherwise fall back to using digitalRead (this seems to be necessary on ESP8266
245246
// right now, perhaps bugs in direct port access functions?).
246247
#else
247248
while (digitalRead(_pin) == level) {
248249
if (count++ >= _maxcycles) {
249-
return 0; // Exceeded timeout, fail.
250+
return TIMEOUT; // Exceeded timeout, fail.
250251
}
251252
}
252253
#endif

0 commit comments

Comments
 (0)