From f2a48d6267b53ffd805f3bc33d3477318271d3ce Mon Sep 17 00:00:00 2001 From: Tony DiCola Date: Mon, 2 Jun 2014 13:41:12 -0700 Subject: [PATCH] Resolve issue #20 by moving delay after 2 second read check to make read return quickly. --- DHT.cpp | 10 ++++++---- examples/DHTtester/DHTtester.ino | 3 +++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/DHT.cpp b/DHT.cpp index a07eeda..fe55016 100644 --- a/DHT.cpp +++ b/DHT.cpp @@ -93,10 +93,8 @@ boolean DHT::read(void) { uint8_t j = 0, i; unsigned long currenttime; - // pull the pin high and wait 250 milliseconds - digitalWrite(_pin, HIGH); - delay(250); - + // Check if sensor was read less than two seconds ago and return early + // to use last reading. currenttime = millis(); if (currenttime < _lastreadtime) { // ie there was a rollover @@ -115,6 +113,10 @@ boolean DHT::read(void) { data[0] = data[1] = data[2] = data[3] = data[4] = 0; + // pull the pin high and wait 250 milliseconds + digitalWrite(_pin, HIGH); + delay(250); + // now pull it low for ~20 milliseconds pinMode(_pin, OUTPUT); digitalWrite(_pin, LOW); diff --git a/examples/DHTtester/DHTtester.ino b/examples/DHTtester/DHTtester.ino index fc17e87..7af6a0b 100644 --- a/examples/DHTtester/DHTtester.ino +++ b/examples/DHTtester/DHTtester.ino @@ -25,6 +25,9 @@ void setup() { } void loop() { + // Wait a few seconds between measurements. + delay(2000); + // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity();