Skip to content

Commit ce05c35

Browse files
committed
Remove serial.print from library and update example error handling. Resolves issue #11 and #13.
1 parent ffac2f1 commit ce05c35

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

DHT.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ float DHT::readTemperature(bool S) {
4646
return f;
4747
}
4848
}
49-
Serial.print("Read fail");
5049
return NAN;
5150
}
5251

@@ -70,7 +69,6 @@ float DHT::readHumidity(void) {
7069
return f;
7170
}
7271
}
73-
Serial.print("Read fail");
7472
return NAN;
7573
}
7674

examples/DHTtester/DHTtester.ino

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,26 @@ void loop() {
3232
float t = dht.readTemperature();
3333
// Read temperature as Fahrenheit
3434
float f = dht.readTemperature(true);
35+
36+
// Check if any reads failed and exit early (to try again).
37+
if (isnan(h) || isnan(t) || isnan(f)) {
38+
Serial.println("Failed to read from DHT sensor!");
39+
return;
40+
}
41+
3542
// Compute heat index
3643
// Must send in temp in Fahrenheit!
3744
float hi = dht.computeHeatIndex(f, h);
3845

39-
// check if returns are valid, if they are NaN (not a number) then something went wrong!
40-
if (isnan(t) || isnan(h)) {
41-
Serial.println("Failed to read from DHT");
42-
} else {
43-
Serial.print("Humidity: ");
44-
Serial.print(h);
45-
Serial.print(" %\t");
46-
Serial.print("Temperature: ");
47-
Serial.print(t);
48-
Serial.print(" *C ");
49-
Serial.print(f);
50-
Serial.print(" *F\t");
51-
Serial.print("Heat index: ");
52-
Serial.print(hi);
53-
Serial.println(" *F");
54-
55-
}
46+
Serial.print("Humidity: ");
47+
Serial.print(h);
48+
Serial.print(" %\t");
49+
Serial.print("Temperature: ");
50+
Serial.print(t);
51+
Serial.print(" *C ");
52+
Serial.print(f);
53+
Serial.print(" *F\t");
54+
Serial.print("Heat index: ");
55+
Serial.print(hi);
56+
Serial.println(" *F");
5657
}

0 commit comments

Comments
 (0)