Skip to content

Commit

Permalink
added force parameter to DHT read function
Browse files Browse the repository at this point in the history
  • Loading branch information
mozzbozz committed Jul 20, 2016
1 parent 9a0003d commit e97ae9e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 5 additions & 2 deletions libraries/DHT/DHT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
2013-06-10: Initial version
2013-06-12: Refactored code
2013-07-01: Add a resetTimer method
2016-07-20: Add force parameter - Torben Woltjen (mozzbozz)
******************************************************************/

#include "DHT.h"
Expand Down Expand Up @@ -108,13 +109,15 @@ const char *DHT::getStatusString() {

#endif

void DHT::readSensor()
void DHT::readSensor(bool force)
{
// Make sure we don't poll the sensor too often
// - Max sample rate DHT11 is 1 Hz (duty cicle 1000 ms)
// - Max sample rate DHT22 is 0.5 Hz (duty cicle 2000 ms)
// If 'force' is true, the user has to take care of this -> this way, the
// microcontroller can be set to sleep where it doesn't increase millis().
unsigned long startTime = millis();
if ( (unsigned long)(startTime - lastReadTime) < (model == DHT11 ? 999L : 1999L) ) {
if ( !force && (unsigned long)(startTime - lastReadTime) < (model == DHT11 ? 999L : 1999L) ) {
return;
}
lastReadTime = startTime;
Expand Down
4 changes: 2 additions & 2 deletions libraries/DHT/DHT.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
2013-06-10: Initial version
2013-06-12: Refactored code
2013-07-01: Add a resetTimer method
2016-07-20: Add force parameter - Torben Woltjen (mozzbozz)
******************************************************************/

#ifndef dht_h
Expand Down Expand Up @@ -58,6 +59,7 @@ class DHT
void setup(uint8_t pin, DHT_MODEL_t model=AUTO_DETECT);
void resetTimer();

void readSensor(bool force=false);
float getTemperature();
float getHumidity();

Expand All @@ -80,8 +82,6 @@ class DHT
static float toCelsius(float fromFahrenheit) { return (fromFahrenheit - 32.0) / 1.8; };

protected:
void readSensor();

float temperature;
float humidity;

Expand Down

0 comments on commit e97ae9e

Please sign in to comment.