Skip to content

Commit ba5e9bd

Browse files
committed
refactored TIMEOUT
1 parent 1f2314c commit ba5e9bd

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

libraries/DHTlib/dht.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//
22
// FILE: dht.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.10
4+
// VERSION: 0.1.11
55
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
66
// URL: http://arduino.cc/playground/Main/DHTLib
77
//
88
// HISTORY:
9+
// 0.1.11 renamed DHTLIB_TIMEOUT
910
// 0.1.10 optimized faster WAKEUP + TIMEOUT
1011
// 0.1.09 optimize size: timeout check + use of mask
1112
// 0.1.08 added formula for timeout based upon clockspeed
@@ -25,14 +26,6 @@
2526

2627
#include "dht.h"
2728

28-
// max timeout is 100usec.
29-
// For a 16Mhz proc that is max 1600 clock cycles
30-
// loops using TIMEOUT use at least 4 clock cycli
31-
// so 100 us takes max 400 loops
32-
// so by dividing F_CPU by 40000 we "fail" as fast as possible
33-
#define TIMEOUT (F_CPU/40000)
34-
35-
3629
/////////////////////////////////////////////////////
3730
//
3831
// PUBLIC
@@ -135,13 +128,13 @@ int dht::read(uint8_t pin, uint8_t wakeupDelay)
135128
pinMode(pin, INPUT);
136129

137130
// GET ACKNOWLEDGE or TIMEOUT
138-
unsigned int loopCnt = TIMEOUT;
131+
unsigned int loopCnt = DHTLIB_TIMEOUT;
139132
while(digitalRead(pin) == LOW)
140133
{
141134
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
142135
}
143136

144-
loopCnt = TIMEOUT;
137+
loopCnt = DHTLIB_TIMEOUT;
145138
while(digitalRead(pin) == HIGH)
146139
{
147140
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
@@ -150,15 +143,15 @@ int dht::read(uint8_t pin, uint8_t wakeupDelay)
150143
// READ THE OUTPUT - 40 BITS => 5 BYTES
151144
for (uint8_t i = 0; i < 40; i++)
152145
{
153-
loopCnt = TIMEOUT;
146+
loopCnt = DHTLIB_TIMEOUT;
154147
while(digitalRead(pin) == LOW)
155148
{
156149
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
157150
}
158151

159152
unsigned long t = micros();
160153

161-
loopCnt = TIMEOUT;
154+
loopCnt = DHTLIB_TIMEOUT;
162155
while(digitalRead(pin) == HIGH)
163156
{
164157
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;

libraries/DHTlib/dht.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: dht.h
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.10
4+
// VERSION: 0.1.11
55
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
66
// URL: http://arduino.cc/playground/Main/DHTLib
77
//
@@ -18,7 +18,7 @@
1818
#include <Arduino.h>
1919
#endif
2020

21-
#define DHT_LIB_VERSION "0.1.10"
21+
#define DHT_LIB_VERSION "0.1.11"
2222

2323
#define DHTLIB_OK 0
2424
#define DHTLIB_ERROR_CHECKSUM -1
@@ -28,6 +28,12 @@
2828
#define DHTLIB_DHT11_WAKEUP 18
2929
#define DHTLIB_DHT22_WAKEUP 1
3030

31+
// max timeout is 100usec.
32+
// For a 16Mhz proc that is max 1600 clock cycles
33+
// loops using TIMEOUT use at least 4 clock cycli
34+
// so 100 us takes max 400 loops
35+
// so by dividing F_CPU by 40000 we "fail" as fast as possible
36+
#define DHTLIB_TIMEOUT (F_CPU/40000)
3137

3238
class dht
3339
{

0 commit comments

Comments
 (0)