mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
+ finalized support for DHT21
+ made TIMEOUT depends on clock ==> F_CPU
This commit is contained in:
parent
9593136bda
commit
c7c5bdf661
@ -22,7 +22,7 @@
|
|||||||
|
|
||||||
#include "dht.h"
|
#include "dht.h"
|
||||||
|
|
||||||
#define TIMEOUT 10000
|
#define TIMEOUT (F_CPU/1600) // unsigned int in code, for higher CPU speeds this might exceed MAXINT.
|
||||||
|
|
||||||
/////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
@ -62,29 +62,7 @@ int dht::read11(uint8_t pin)
|
|||||||
// DHTLIB_ERROR_TIMEOUT
|
// DHTLIB_ERROR_TIMEOUT
|
||||||
int dht::read21(uint8_t pin)
|
int dht::read21(uint8_t pin)
|
||||||
{
|
{
|
||||||
// READ VALUES
|
return read22(pin); // dataformat identical to DHT22
|
||||||
int rv = read(pin);
|
|
||||||
if (rv != DHTLIB_OK)
|
|
||||||
{
|
|
||||||
humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
|
|
||||||
temperature = DHTLIB_INVALID_VALUE; // invalid value
|
|
||||||
return rv; // propagate error value
|
|
||||||
}
|
|
||||||
|
|
||||||
// CONVERT AND STORE
|
|
||||||
humidity = bits[0] + bits[1] * 0.00390625; // == 1/256.0
|
|
||||||
temperature = (bits[2] & 0x7F) + bits[3] * 0.00390625;
|
|
||||||
|
|
||||||
if (bits[2] & 0x80) // negative temperature
|
|
||||||
{
|
|
||||||
temperature = -temperature;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TEST CHECKSUM
|
|
||||||
uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
|
|
||||||
if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
|
|
||||||
|
|
||||||
return DHTLIB_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// return values:
|
// return values:
|
||||||
|
@ -13,7 +13,8 @@
|
|||||||
dht DHT;
|
dht DHT;
|
||||||
|
|
||||||
#define DHT11_PIN 4
|
#define DHT11_PIN 4
|
||||||
#define DHT22_PIN 5
|
#define DHT21_PIN 5
|
||||||
|
#define DHT22_PIN 6
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
@ -55,7 +56,7 @@ void loop()
|
|||||||
|
|
||||||
// READ DATA
|
// READ DATA
|
||||||
Serial.print("DHT21, \t");
|
Serial.print("DHT21, \t");
|
||||||
int chk = DHT.read21(DHT21_PIN);
|
chk = DHT.read21(DHT21_PIN);
|
||||||
switch (chk)
|
switch (chk)
|
||||||
{
|
{
|
||||||
case DHTLIB_OK:
|
case DHTLIB_OK:
|
Loading…
Reference in New Issue
Block a user