Pages: [1]   Go Down
Author Topic: Stumped reading mulitple DS18B20's  (Read 864 times)
0 Members and 1 Guest are viewing this topic.
UK
Offline Offline
Sr. Member
****
Karma: 0
Posts: 276
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, now this should all work as fars as I can tell but all five sensor values are coming out with the same value - ie that of S1..... any idea what daft mistake I have made?

Code:
#include <OneWire.h>
#include <DallasTemperature.h>

float S1;
float S2;
float S3;
float S4;
float S5;

#define ONE_WIRE_BUS 7
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

DeviceAddress Sensor1 = { 0x28, 0x64, 0xDB, 0x8B, 0x03, 0x00, 0x00, 0x86 };
DeviceAddress Sensor2 = { 0x28, 0x0C, 0x06, 0x8C, 0x03, 0x00, 0x00, 0xD0 };
DeviceAddress Sensor3 = { 0x28, 0xCA, 0xD9, 0x8B, 0x03, 0x00, 0x00, 0x4A };
DeviceAddress Sensor4 = { 0x28, 0xB6, 0x02, 0x8C, 0x03, 0x00, 0x00, 0x07 };
DeviceAddress Sensor5 = { 0x28, 0x11, 0xDA, 0x8B, 0x03, 0x00, 0x00, 0x38 };


void setup ()
{       
     sensors.begin();
     
     if (!sensors.getAddress(Sensor1, 0)) error();
     if (!sensors.getAddress(Sensor2, 0)) error();
     if (!sensors.getAddress(Sensor3, 0)) error();
     if (!sensors.getAddress(Sensor4, 0)) error();
     if (!sensors.getAddress(Sensor5, 0)) error();
         
     sensors.setResolution(Sensor1, 12); // set the resolution
     sensors.setResolution(Sensor2, 12); // set the resolution
     sensors.setResolution(Sensor3, 12); // set the resolution
     sensors.setResolution(Sensor4, 12); // set the resolution
     sensors.setResolution(Sensor5, 12); // set the resolution

void loop ()
{
 sensors.requestTemperatures();
  S1 = sensors.getTempC(Sensor1);
  S2 = sensors.getTempC(Sensor2);
  S3 = sensors.getTempC(Sensor3);
  S4 = sensors.getTempC(Sensor4);
  S5 = sensors.getTempC(Sensor5);
}
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 50
Posts: 3426
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Where's the code that tells you they're the same - the issue might be there.
Logged

UK
Offline Offline
Sr. Member
****
Karma: 0
Posts: 276
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Bill, I stripped the code out to make it easy to read, the sensor floats S1 through S5 are simply printed to serial.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 50
Posts: 3426
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This may be your problem:
Code:
     if (!sensors.getAddress(Sensor1, 0)) error();
     if (!sensors.getAddress(Sensor2, 0)) error();
     if (!sensors.getAddress(Sensor3, 0)) error();
     if (!sensors.getAddress(Sensor4, 0)) error();
     if (!sensors.getAddress(Sensor5, 0)) error();

I suspect that getAddress gets the address at the given index. Since you've provided the same index in each case, your addresses are being overwritten with the 0th device on the bus. Since you already know the addresses, these calls are superfluous anyway.
Logged

0
Online Online
Shannon Member
****
Karma: 162
Posts: 10472
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Indeed that is the explanation - getAddress re-searches the address space on each call, but skips as many devices as the value of
its second argument - so if you call it with 0 every time it always finds the same device (overwriting the first argument with the
discovered address).

I would comment out all those calls and see what happens.
Logged

[ I won't respond to messages, use the forum please ]

UK
Offline Offline
Sr. Member
****
Karma: 0
Posts: 276
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

FAB - thanks folks, you got it.

This seems to work - hadn't twiggged the sensor indexing - I can see it clearly now you have pointed it out :-)

Code:
     if (!sensors.getAddress(Sensor1, 0)) error();
     if (!sensors.getAddress(Sensor2, 1)) error();
     if (!sensors.getAddress(Sensor3, 2)) error();
     if (!sensors.getAddress(Sensor4, 3)) error();
     if (!sensors.getAddress(Sensor5, 4)) error();

« Last Edit: December 11, 2012, 03:55:21 am by point5 » Logged

Pages: [1]   Go Up
Jump to: