From dc9db2aef64b1ef45d938a8678bb53d0e74170e6 Mon Sep 17 00:00:00 2001 From: marcel2012 Date: Tue, 27 Jun 2017 02:18:26 +0200 Subject: [PATCH] return; bug fixed --- DHT.cpp | 28 ++++++++++++---------------- DHT.h | 2 +- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/DHT.cpp b/DHT.cpp index 525e32a..dfcbde3 100644 --- a/DHT.cpp +++ b/DHT.cpp @@ -37,11 +37,10 @@ void DHT::setup(uint8_t pin, DHT_MODEL_t model) if ( model == AUTO_DETECT) { DHT::model = DHT22; readSensor(); - if ( error == ERROR_TIMEOUT ) { + if ( error == ERROR_TIMEOUT ) DHT::model = DHT11; // Warning: in case we auto detect a DHT11, you should wait at least 1000 msec // before your first read request. Otherwise you will get a time out error. - } } } @@ -108,15 +107,15 @@ const char *DHT::getStatusString() { #endif -void DHT::readSensor() +bool DHT::readSensor() { // 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) unsigned long startTime = millis(); - if ( (unsigned long)(startTime - lastReadTime) < (model == DHT11 ? 999L : 1999L) ) { - return; - } + if ( (unsigned long)(startTime - lastReadTime) < (model == DHT11 ? 999L : 1999L) ) + return false; + lastReadTime = startTime; temperature = NAN; @@ -126,13 +125,11 @@ void DHT::readSensor() digitalWrite(pin, LOW); // Send start signal pinMode(pin, OUTPUT); - if ( model == DHT11 ) { + if ( model == DHT11 ) delay(18); - } - else { + else // This will fail for a DHT11 - that's how we can detect such a device delayMicroseconds(800); - } pinMode(pin, INPUT); digitalWrite(pin, HIGH); // Switch bus to receive data @@ -154,7 +151,7 @@ void DHT::readSensor() age = (unsigned long)(micros() - startTime); if ( age > 90 ) { error = ERROR_TIMEOUT; - return; + return false; } } while ( digitalRead(pin) == (i & 1) ? HIGH : LOW ); @@ -164,9 +161,8 @@ void DHT::readSensor() data <<= 1; // A zero max 30 usecs, a one at least 68 usecs. - if ( age > 30 ) { + if ( age > 30 ) data |= 1; // we got a one - } } switch ( i ) { @@ -184,7 +180,7 @@ void DHT::readSensor() if ( (byte)(((byte)rawHumidity) + (rawHumidity >> 8) + ((byte)rawTemperature) + (rawTemperature >> 8)) != data ) { error = ERROR_CHECKSUM; - return; + return false; } // Store readings @@ -196,11 +192,11 @@ void DHT::readSensor() else { humidity = rawHumidity * 0.1; - if ( rawTemperature & 0x8000 ) { + if ( rawTemperature & 0x8000 ) rawTemperature = -(int16_t)(rawTemperature & 0x7FFF); - } temperature = ((int16_t)rawTemperature) * 0.1; } error = ERROR_NONE; + return true; } diff --git a/DHT.h b/DHT.h index 9e24518..361b57b 100644 --- a/DHT.h +++ b/DHT.h @@ -80,7 +80,7 @@ class DHT static float toCelsius(float fromFahrenheit) { return (fromFahrenheit - 32.0) / 1.8; }; protected: - void readSensor(); + bool readSensor(); float temperature; float humidity;