Skip to content

Commit 8cb1bd9

Browse files
authored
Merge pull request milesburton#124 from Jakeler/master
Add error handling to examples
2 parents 1050b66 + 5f42de5 commit 8cb1bd9

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

examples/Multiple/Multiple.pde

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ void printAddress(DeviceAddress deviceAddress)
102102
void printTemperature(DeviceAddress deviceAddress)
103103
{
104104
float tempC = sensors.getTempC(deviceAddress);
105+
if(tempC == DEVICE_DISCONNECTED_C)
106+
{
107+
Serial.println("Error: Could not read temperature data");
108+
return;
109+
}
105110
Serial.print("Temp C: ");
106111
Serial.print(tempC);
107112
Serial.print(" Temp F: ");

examples/Simple/Simple.pde

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ void loop(void)
3636
Serial.println("DONE");
3737
// After we got the temperatures, we can print them here.
3838
// 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+
}

examples/Single/Single.pde

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ void printTemperature(DeviceAddress deviceAddress)
8585

8686
// method 2 - faster
8787
float tempC = sensors.getTempC(deviceAddress);
88+
if(tempC == DEVICE_DISCONNECTED_C)
89+
{
90+
Serial.println("Error: Could not read temperature data");
91+
return;
92+
}
8893
Serial.print("Temp C: ");
8994
Serial.print(tempC);
9095
Serial.print(" Temp F: ");
@@ -113,4 +118,4 @@ void printAddress(DeviceAddress deviceAddress)
113118
if (deviceAddress[i] < 16) Serial.print("0");
114119
Serial.print(deviceAddress[i], HEX);
115120
}
116-
}
121+
}

examples/Tester/Tester.pde

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void setup(void)
5757
// set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions)
5858
sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
5959

60-
Serial.print("Resolution actually set to: ");
60+
Serial.print("Resolution actually set to: ");
6161
Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
6262
Serial.println();
6363
}else{
@@ -80,6 +80,11 @@ void printTemperature(DeviceAddress deviceAddress)
8080

8181
// method 2 - faster
8282
float tempC = sensors.getTempC(deviceAddress);
83+
if(tempC == DEVICE_DISCONNECTED_C)
84+
{
85+
Serial.println("Error: Could not read temperature data");
86+
return;
87+
}
8388
Serial.print("Temp C: ");
8489
Serial.print(tempC);
8590
Serial.print(" Temp F: ");
@@ -121,4 +126,4 @@ void printAddress(DeviceAddress deviceAddress)
121126
if (deviceAddress[i] < 16) Serial.print("0");
122127
Serial.print(deviceAddress[i], HEX);
123128
}
124-
}
129+
}

0 commit comments

Comments
 (0)