From ce05c3536cabe3091f1915063a9675f32e61086c Mon Sep 17 00:00:00 2001 From: Tony DiCola Date: Mon, 2 Jun 2014 13:03:27 -0700 Subject: [PATCH] Remove serial.print from library and update example error handling. Resolves issue #11 and #13. --- DHT.cpp | 2 -- examples/DHTtester/DHTtester.ino | 35 ++++++++++++++++---------------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/DHT.cpp b/DHT.cpp index 84d822d..0033ac8 100644 --- a/DHT.cpp +++ b/DHT.cpp @@ -46,7 +46,6 @@ float DHT::readTemperature(bool S) { return f; } } - Serial.print("Read fail"); return NAN; } @@ -70,7 +69,6 @@ float DHT::readHumidity(void) { return f; } } - Serial.print("Read fail"); return NAN; } diff --git a/examples/DHTtester/DHTtester.ino b/examples/DHTtester/DHTtester.ino index 56874ab..fc17e87 100644 --- a/examples/DHTtester/DHTtester.ino +++ b/examples/DHTtester/DHTtester.ino @@ -32,25 +32,26 @@ void loop() { float t = dht.readTemperature(); // Read temperature as Fahrenheit float f = dht.readTemperature(true); + + // Check if any reads failed and exit early (to try again). + if (isnan(h) || isnan(t) || isnan(f)) { + Serial.println("Failed to read from DHT sensor!"); + return; + } + // Compute heat index // Must send in temp in Fahrenheit! float hi = dht.computeHeatIndex(f, h); - // check if returns are valid, if they are NaN (not a number) then something went wrong! - if (isnan(t) || isnan(h)) { - Serial.println("Failed to read from DHT"); - } else { - Serial.print("Humidity: "); - Serial.print(h); - Serial.print(" %\t"); - Serial.print("Temperature: "); - Serial.print(t); - Serial.print(" *C "); - Serial.print(f); - Serial.print(" *F\t"); - Serial.print("Heat index: "); - Serial.print(hi); - Serial.println(" *F"); - - } + Serial.print("Humidity: "); + Serial.print(h); + Serial.print(" %\t"); + Serial.print("Temperature: "); + Serial.print(t); + Serial.print(" *C "); + Serial.print(f); + Serial.print(" *F\t"); + Serial.print("Heat index: "); + Serial.print(hi); + Serial.println(" *F"); }