Skip to content

Commit c7c5bdf

Browse files
committed
+ finalized support for DHT21
+ made TIMEOUT depends on clock ==> F_CPU
1 parent 9593136 commit c7c5bdf

2 files changed

Lines changed: 5 additions & 26 deletions

File tree

libraries/DHTlib/dht.cpp

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
#include "dht.h"
2424

25-
#define TIMEOUT 10000
25+
#define TIMEOUT (F_CPU/1600) // unsigned int in code, for higher CPU speeds this might exceed MAXINT.
2626

2727
/////////////////////////////////////////////////////
2828
//
@@ -62,29 +62,7 @@ int dht::read11(uint8_t pin)
6262
// DHTLIB_ERROR_TIMEOUT
6363
int dht::read21(uint8_t pin)
6464
{
65-
// READ VALUES
66-
int rv = read(pin);
67-
if (rv != DHTLIB_OK)
68-
{
69-
humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
70-
temperature = DHTLIB_INVALID_VALUE; // invalid value
71-
return rv; // propagate error value
72-
}
73-
74-
// CONVERT AND STORE
75-
humidity = bits[0] + bits[1] * 0.00390625; // == 1/256.0
76-
temperature = (bits[2] & 0x7F) + bits[3] * 0.00390625;
77-
78-
if (bits[2] & 0x80) // negative temperature
79-
{
80-
temperature = -temperature;
81-
}
82-
83-
// TEST CHECKSUM
84-
uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
85-
if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
86-
87-
return DHTLIB_OK;
65+
return read22(pin); // dataformat identical to DHT22
8866
}
8967

9068
// return values:

libraries/DHTlib/examples/dht_test1/dht_test1.pde renamed to libraries/DHTlib/examples/dht_test1/dht_test1.ino

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
dht DHT;
1414

1515
#define DHT11_PIN 4
16-
#define DHT22_PIN 5
16+
#define DHT21_PIN 5
17+
#define DHT22_PIN 6
1718

1819
void setup()
1920
{
@@ -55,7 +56,7 @@ void loop()
5556

5657
// READ DATA
5758
Serial.print("DHT21, \t");
58-
int chk = DHT.read21(DHT21_PIN);
59+
chk = DHT.read21(DHT21_PIN);
5960
switch (chk)
6061
{
6162
case DHTLIB_OK:

0 commit comments

Comments
 (0)