File tree Expand file tree Collapse file tree 4 files changed +31
-6
lines changed Expand file tree Collapse file tree 4 files changed +31
-6
lines changed Original file line number Diff line number Diff line change @@ -102,6 +102,11 @@ void printAddress(DeviceAddress deviceAddress)
102
102
void printTemperature(DeviceAddress deviceAddress)
103
103
{
104
104
float tempC = sensors. getTempC(deviceAddress);
105
+ if (tempC == DEVICE_DISCONNECTED_C )
106
+ {
107
+ Serial . println(" Error: Could not read temperature data" );
108
+ return ;
109
+ }
105
110
Serial . print(" Temp C: " );
106
111
Serial . print(tempC);
107
112
Serial . print(" Temp F: " );
Original file line number Diff line number Diff line change @@ -36,6 +36,16 @@ void loop(void)
36
36
Serial . println(" DONE" );
37
37
// After we got the temperatures, we can print them here.
38
38
// We use the function ByIndex, and as an example get the temperature from the first sensor only.
39
- Serial . print(" Temperature for the device 1 (index 0) is: " );
40
- Serial . println(sensors. getTempCByIndex(0 ));
41
- }
39
+ float tempC = sensors. getTempCByIndex(0 );
40
+
41
+ // Check if reading was successful
42
+ if (tempC != DEVICE_DISCONNECTED_C )
43
+ {
44
+ Serial . print(" Temperature for the device 1 (index 0) is: " );
45
+ Serial . println(tempC);
46
+ }
47
+ else
48
+ {
49
+ Serial . println(" Error: Could not read temperature data" );
50
+ }
51
+ }
Original file line number Diff line number Diff line change @@ -85,6 +85,11 @@ void printTemperature(DeviceAddress deviceAddress)
85
85
86
86
// method 2 - faster
87
87
float tempC = sensors. getTempC(deviceAddress);
88
+ if (tempC == DEVICE_DISCONNECTED_C )
89
+ {
90
+ Serial . println(" Error: Could not read temperature data" );
91
+ return ;
92
+ }
88
93
Serial . print(" Temp C: " );
89
94
Serial . print(tempC);
90
95
Serial . print(" Temp F: " );
@@ -113,4 +118,4 @@ void printAddress(DeviceAddress deviceAddress)
113
118
if (deviceAddress[i] < 16 ) Serial . print(" 0" );
114
119
Serial . print(deviceAddress[i], HEX );
115
120
}
116
- }
121
+ }
Original file line number Diff line number Diff line change @@ -57,7 +57,7 @@ void setup(void)
57
57
// set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions)
58
58
sensors. setResolution(tempDeviceAddress, TEMPERATURE_PRECISION );
59
59
60
- Serial . print(" Resolution actually set to: " );
60
+ Serial . print(" Resolution actually set to: " );
61
61
Serial . print(sensors. getResolution(tempDeviceAddress), DEC );
62
62
Serial . println();
63
63
}else {
@@ -80,6 +80,11 @@ void printTemperature(DeviceAddress deviceAddress)
80
80
81
81
// method 2 - faster
82
82
float tempC = sensors. getTempC(deviceAddress);
83
+ if (tempC == DEVICE_DISCONNECTED_C )
84
+ {
85
+ Serial . println(" Error: Could not read temperature data" );
86
+ return ;
87
+ }
83
88
Serial . print(" Temp C: " );
84
89
Serial . print(tempC);
85
90
Serial . print(" Temp F: " );
@@ -121,4 +126,4 @@ void printAddress(DeviceAddress deviceAddress)
121
126
if (deviceAddress[i] < 16 ) Serial . print(" 0" );
122
127
Serial . print(deviceAddress[i], HEX );
123
128
}
124
- }
129
+ }
You can’t perform that action at this time.
0 commit comments