Skip to content

Commit

Permalink
Resolve issue adafruit#20 by moving delay after 2 second read check t…
Browse files Browse the repository at this point in the history
…o make read return quickly.
  • Loading branch information
tdicola committed Jun 2, 2014
1 parent 9611395 commit f2a48d6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
10 changes: 6 additions & 4 deletions DHT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions examples/DHTtester/DHTtester.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f2a48d6

Please sign in to comment.