From 04905bc5cd829ad13e299240aa9b202ca96fae94 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Fri, 24 Jul 2015 17:37:24 +0200 Subject: [PATCH] Add force parameter to read methods This allows forcing a read, even if the previous read was less than 2 seconds ago. This is useful in cases where the millis() timer is not reliable, such as when sleeping. In this case, it is up to the caller to ensure that at least 2 seconds elapse between calls with force set to true. --- DHT.cpp | 10 +++++----- DHT.h | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/DHT.cpp b/DHT.cpp index 79f6a40..659dd97 100644 --- a/DHT.cpp +++ b/DHT.cpp @@ -27,10 +27,10 @@ void DHT::begin(void) { } //boolean S == Scale. True == Fahrenheit; False == Celcius -float DHT::readTemperature(bool S) { +float DHT::readTemperature(bool S, bool force) { float f = NAN; - if (read()) { + if (read(force)) { switch (_type) { case DHT11: f = data[2]; @@ -64,7 +64,7 @@ float DHT::convertFtoC(float f) { return (f - 32) * 5 / 9; } -float DHT::readHumidity(void) { +float DHT::readHumidity(bool force) { float f = NAN; if (read()) { switch (_type) { @@ -113,7 +113,7 @@ 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 // to use last reading. uint32_t currenttime = millis(); @@ -121,7 +121,7 @@ boolean DHT::read(void) { // ie there was a rollover _lastreadtime = 0; } - if (!_firstreading && ((currenttime - _lastreadtime) < 2000)) { + if (!force && !_firstreading && ((currenttime - _lastreadtime) < 2000)) { return _lastresult; // return last correct measurement } _firstreading = false; diff --git a/DHT.h b/DHT.h index dd4d1f5..5040fa3 100644 --- a/DHT.h +++ b/DHT.h @@ -39,12 +39,12 @@ class DHT { public: DHT(uint8_t pin, uint8_t type, uint8_t count=6); void begin(void); - float readTemperature(bool S=false); + float readTemperature(bool S=false, bool force=false); float convertCtoF(float); float convertFtoC(float); float computeHeatIndex(float temperature, float percentHumidity, bool isFahrenheit=true); - float readHumidity(void); - boolean read(void); + float readHumidity(bool force=false); + boolean read(bool force=false); private: uint8_t data[6];