Merge branch 'matthijskooijman-fixes'

This commit is contained in:
Tony DiCola 2015-09-15 12:26:54 -07:00
commit 6c0c723907
3 changed files with 19 additions and 22 deletions

30
DHT.cpp
View File

@ -6,10 +6,11 @@ written by Adafruit Industries
#include "DHT.h" #include "DHT.h"
#define MIN_INTERVAL 2000
DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) { DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
_pin = pin; _pin = pin;
_type = type; _type = type;
_firstreading = true;
_bit = digitalPinToBitMask(pin); _bit = digitalPinToBitMask(pin);
_port = digitalPinToPort(pin); _port = digitalPinToPort(pin);
_maxcycles = microsecondsToClockCycles(1000); // 1 millisecond timeout for _maxcycles = microsecondsToClockCycles(1000); // 1 millisecond timeout for
@ -20,17 +21,19 @@ DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
void DHT::begin(void) { void DHT::begin(void) {
// set up the pins! // set up the pins!
pinMode(_pin, INPUT); pinMode(_pin, INPUT_PULLUP);
digitalWrite(_pin, HIGH); // Using this value makes sure that millis() - lastreadtime will be
_lastreadtime = 0; // >= MIN_INTERVAL right away. Note that this assignment wraps around,
// but so will the subtraction.
_lastreadtime = -MIN_INTERVAL;
DEBUG_PRINT("Max clock cycles: "); DEBUG_PRINTLN(_maxcycles, DEC); DEBUG_PRINT("Max clock cycles: "); DEBUG_PRINTLN(_maxcycles, DEC);
} }
//boolean S == Scale. True == Fahrenheit; False == Celcius //boolean S == Scale. True == Fahrenheit; False == Celcius
float DHT::readTemperature(bool S) { float DHT::readTemperature(bool S, bool force) {
float f = NAN; float f = NAN;
if (read()) { if (read(force)) {
switch (_type) { switch (_type) {
case DHT11: case DHT11:
f = data[2]; f = data[2];
@ -64,7 +67,7 @@ float DHT::convertFtoC(float f) {
return (f - 32) * 5 / 9; return (f - 32) * 5 / 9;
} }
float DHT::readHumidity(void) { float DHT::readHumidity(bool force) {
float f = NAN; float f = NAN;
if (read()) { if (read()) {
switch (_type) { switch (_type) {
@ -113,19 +116,14 @@ float DHT::computeHeatIndex(float temperature, float percentHumidity, bool isFah
} }
} }
boolean DHT::read(void) { boolean DHT::read(bool force) {
// Check if sensor was read less than two seconds ago and return early // Check if sensor was read less than two seconds ago and return early
// to use last reading. // to use last reading.
uint32_t currenttime = millis(); uint32_t currenttime = millis();
if (currenttime < _lastreadtime) { if (!force && ((currenttime - _lastreadtime) < 2000)) {
// ie there was a rollover
_lastreadtime = 0;
}
if (!_firstreading && ((currenttime - _lastreadtime) < 2000)) {
return _lastresult; // return last correct measurement return _lastresult; // return last correct measurement
} }
_firstreading = false; _lastreadtime = currenttime;
_lastreadtime = millis();
// Reset 40 bits of received data to zero. // Reset 40 bits of received data to zero.
data[0] = data[1] = data[2] = data[3] = data[4] = 0; data[0] = data[1] = data[2] = data[3] = data[4] = 0;
@ -154,7 +152,7 @@ boolean DHT::read(void) {
delayMicroseconds(40); delayMicroseconds(40);
// Now start reading the data line to get the value from the DHT sensor. // Now start reading the data line to get the value from the DHT sensor.
pinMode(_pin, INPUT); pinMode(_pin, INPUT_PULLUP);
delayMicroseconds(10); // Delay a bit to let sensor pull data line low. delayMicroseconds(10); // Delay a bit to let sensor pull data line low.
// First expect a low signal for ~80 microseconds followed by a high signal // First expect a low signal for ~80 microseconds followed by a high signal

9
DHT.h
View File

@ -39,18 +39,17 @@ class DHT {
public: public:
DHT(uint8_t pin, uint8_t type, uint8_t count=6); DHT(uint8_t pin, uint8_t type, uint8_t count=6);
void begin(void); void begin(void);
float readTemperature(bool S=false); float readTemperature(bool S=false, bool force=false);
float convertCtoF(float); float convertCtoF(float);
float convertFtoC(float); float convertFtoC(float);
float computeHeatIndex(float temperature, float percentHumidity, bool isFahrenheit=true); float computeHeatIndex(float temperature, float percentHumidity, bool isFahrenheit=true);
float readHumidity(void); float readHumidity(bool force=false);
boolean read(void); boolean read(bool force=false);
private: private:
uint8_t data[6]; uint8_t data[5];
uint8_t _pin, _type, _bit, _port; uint8_t _pin, _type, _bit, _port;
uint32_t _lastreadtime, _maxcycles; uint32_t _lastreadtime, _maxcycles;
bool _firstreading;
bool _lastresult; bool _lastresult;
uint32_t expectPulse(bool level); uint32_t expectPulse(bool level);

View File

@ -1,5 +1,5 @@
name=DHT sensor library name=DHT sensor library
version=1.1.1 version=1.2.0
author=Adafruit author=Adafruit
maintainer=Adafruit <info@adafruit.com> maintainer=Adafruit <info@adafruit.com>
sentence=Arduino library for DHT11, DHT22, etc Temp & Humidity Sensors sentence=Arduino library for DHT11, DHT22, etc Temp & Humidity Sensors