milesburton / Arduino-Temperature-Control-Library

Arduino Temperature Library
https://www.milesburton.com/w/index.php/Dallas_Temperature_Control_Library
973 stars 487 forks source link

Only updates the supplied deviceAddress if a valid index is specified #251

Open mitchyboy9 opened 6 months ago

mitchyboy9 commented 6 months ago

This change addresses #229. I ran into the same problem described in that issue while using the Multiple.ino example.

I only had 1 sensor connected, but was confusingly getting temperature readings for both the sensors declared in the example. I had received the "Unable to find address for Device 1" output from the Setup() method but the code used the address for my single sensor as the address for the nonexistent sensor.

Output from Multiple.ino example (with only 1 DS18B20 connected) before this change. Notice that device address 2861640AF24BE40Dis used twice:

Dallas Temperature IC Control Library Demo
Locating devices...Found 1 devices.
Parasite power is: OFF
Unable to find address for Device 1
Device 0 Address: 2861640AF24BE40D
Device 1 Address: 2861640AF24BE40D
Device 0 Resolution: 12
Device 1 Resolution: 12
Requesting temperatures...DONE
Device Address: 2861640AF24BE40D Temp C: 23.50 Temp F: 74.30
Device Address: 2861640AF24BE40D Temp C: 23.50 Temp F: 74.30

Output from Multiple.ino example (with only 1 DS18B20 connected) after this change. The nonexistent sensor is now given the address 0000000000000000:

> Dallas Temperature IC Control Library Demo
> Locating devices...Found 1 devices.
> Parasite power is: OFF
> Unable to find address for Device 1
> Device 0 Address: 2861640AF24BE40D
> Device 1 Address: 0000000000000000
> Device 0 Resolution: 12
> Device 1 Resolution: 0
> Requesting temperatures...DONE
> Device Address: 2861640AF24BE40D Temp C: 23.56 Temp F: 74.41
> Device Address: 0000000000000000 Error: Could not read temperature data
RobTillaart commented 6 months ago

Thanks, Not reviewed it in depth. One question pops up. what happens if devices is not set before call to validAddress()? Can that happen?

mitchyboy9 commented 6 months ago

This is a good concern, but I think we're fine because devices is initialised to zero when setOneWire is called, which is called by the constructor.