Use of 'for' loop and array index with OneWire/Dallastemp library

Is there a possibility to set the resolution, request temp, set alarm, etc... of all seven ds18b20 temp sensors (see code) by means of a for.... loop? I know there is already some kind of an array with all the sensors with an index assigned to them but I don't know how to use it.....?
I just started with programming so if somebody can point me already in a good direction...

Thanks in advance

//Included libraries
#include <OneWire.h>
#include <DallasTemperature.h>

////////////////////////////////////////////////////////////////////////////////////////////////////
//      Global variables                            /
////////////////////////////////////////////////////////////////////////////////////////////////////

byte alarmStatus, sensorStatus, resolutiontest;

////////////////////////////////////////////////////////////////////////////////////////////////////
//      Data wire for temp sensors is plugged into pin 3 on the Arduino                            /
////////////////////////////////////////////////////////////////////////////////////////////////////

#define ONE_WIRE_BUS 3

////////////////////////////////////////////////////////////////////////////////////////////////////
//      Setup a oneWire instance to communicate with any OneWire devices                           /
////////////////////////////////////////////////////////////////////////////////////////////////////

OneWire oneWire(ONE_WIRE_BUS);

////////////////////////////////////////////////////////////////////////////////////////////////////
//      Pass the oneWire references to Dallas Temperature library                                  /
////////////////////////////////////////////////////////////////////////////////////////////////////

DallasTemperature sensors(&oneWire);

////////////////////////////////////////////////////////////////////////////////////////////////////
//      Manually assign sensor addresses                                                           /
////////////////////////////////////////////////////////////////////////////////////////////////////

DeviceAddress sensorINL1 = { 0x28, 0xC4, 0xEF, 0xC2, 0x03, 0x00, 0x00, 0x32 };
DeviceAddress sensorINL2 = { 0x28, 0xAC, 0xF5, 0xC2, 0x03, 0x00, 0x00, 0x91 };
DeviceAddress sensorINL3 = { 0x28, 0xC1, 0x02, 0xC3, 0x03, 0x00, 0x00, 0x60 };
DeviceAddress sensorOUTL1 = { 0x28, 0xC1, 0x19, 0xC3, 0x03, 0x00, 0x00, 0x6C };
DeviceAddress sensorOUTL2 = { 0x28, 0x61, 0xE7, 0xC2, 0x03, 0x00, 0x00, 0xBB };
DeviceAddress sensorOUTL3 = { 0x28, 0x79, 0x0A, 0xC3, 0x03, 0x00, 0x00, 0xF8 };
DeviceAddress sensorAmbient = { 0x28, 0xAD, 0x04, 0xC3, 0x03, 0x00, 0x00, 0x42 };

////////////////////////////////////////////////////////////////////////////////////////////////////
//      Setup                                                                                      /
////////////////////////////////////////////////////////////////////////////////////////////////////

void setup(void){

  Serial.begin(9600);// start serial port

  sensors.begin();// Start up library

  setResol(11);// Set resolution (12bit)
  setAlarm(25);// Set alarm setpoint in °C (int, no float)
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
//      Loop                                                                                        /
/////////////////////////////////////////////////////////////////////////////////////////////////////

void loop(void){
  delay(750);
  Serial.print("Getting temperatures...\n\r");
  sensors.requestTemperatures();
 
  getTemperature(sensorINL1);
  Serial.print("\n\r"); 
  
  getTemperature(sensorINL2);
  Serial.print("\n\r");  
  getTemperature(sensorINL3);
  Serial.print("\n\r\n\r");
  getTemperature(sensorOUTL1);
  Serial.print("\n\r");  
  getTemperature(sensorOUTL2);
  Serial.print("\n\r");  
  getTemperature(sensorOUTL3);
  Serial.print("\n\r\n\r");
  getTemperature(sensorAmbient);
  Serial.print("\n\r\n\r");
  checkAlarm(sensorAmbient);
  sensorStatusWord;
  Serial.print(sensorStatus);
  Serial.print("\n\r");

}...............................................etc

Is there a possibility to set the resolution, request temp, set alarm, etc... of all seven ds18b20 temp sensors (see code) by means of a for.... loop?

Yes. You can use a for loop to iterate over an array of objects.

DeviceAddress sensorINL1 = { 0x28, 0xC4, 0xEF, 0xC2, 0x03, 0x00, 0x00, 0x32 };
DeviceAddress sensorINL2 = { 0x28, 0xAC, 0xF5, 0xC2, 0x03, 0x00, 0x00, 0x91 };
DeviceAddress sensorINL3 = { 0x28, 0xC1, 0x02, 0xC3, 0x03, 0x00, 0x00, 0x60 };
DeviceAddress sensorOUTL1 = { 0x28, 0xC1, 0x19, 0xC3, 0x03, 0x00, 0x00, 0x6C };
DeviceAddress sensorOUTL2 = { 0x28, 0x61, 0xE7, 0xC2, 0x03, 0x00, 0x00, 0xBB };
DeviceAddress sensorOUTL3 = { 0x28, 0x79, 0x0A, 0xC3, 0x03, 0x00, 0x00, 0xF8 };
DeviceAddress sensorAmbient = { 0x28, 0xAD, 0x04, 0xC3, 0x03, 0x00, 0x00, 0x42 };

Too bad you don't have an array of objects. Might I suggest that you make an array of objects.

did you mean something like this... (I don't know much about object oriented programming....)?

struct Sensor
{
byte index;
uint8_t deviceAddress[8];
float tempReading;
byte resolution;
int highAlarm;
};


Sensor sensorINL1, sensorINL2, sensorINL3, sensorOUTL1, sensorOUTL2, sensorOUTL3, sensorAmbient;   // declaration

sensorINL1.index = 0;
sensorINL1.deviceAddress = { 0x28, 0xC4, 0xEF, 0xC2, 0x03, 0x00, 0x00, 0x32 };
DeviceAddress sensorINL1.deviceAddress ;

sensorINL2.index = 1;
sensorINL2.deviceAddress = { 0x28, 0xAC, 0xF5, 0xC2, 0x03, 0x00, 0x00, 0x91 };
DeviceAddress sensorINL2.deviceAddress ;

etc......

...later on I can use all these created data members in the program, correct?
I still have to try it out but maybe some additional info would be already very helpful.
thx

did you mean something like this...

I still don't see any arrays.

I don't know much about object oriented programming.

Arrays and object oriented programming have as much in common as potato chips and horseradish.

Sorry that my C/C++ - slang is still not developed that well....

... do you mean I have to make a multidimensional array or ...? Because deviceAddress is already an array of 8 bytes..... (typedef uint8_t DeviceAddress[8]; )

Did you mean something like this?

byte sensor[7][8] = 
{
  { 0x28, 0xC4, 0xEF, 0xC2, 0x03, 0x00, 0x00, 0x32 },
  { 0x28, 0xAC, 0xF5, 0xC2, 0x03, 0x00, 0x00, 0x91 },
  { 0x28, 0xC1, 0x02, 0xC3, 0x03, 0x00, 0x00, 0x60 },
  { 0x28, 0xC1, 0x19, 0xC3, 0x03, 0x00, 0x00, 0x6C },
  { 0x28, 0x61, 0xE7, 0xC2, 0x03, 0x00, 0x00, 0xBB },
  { 0x28, 0x79, 0x0A, 0xC3, 0x03, 0x00, 0x00, 0xF8 },
  { 0x28, 0xAD, 0x04, 0xC3, 0x03, 0x00, 0x00, 0x42 }
}

If so, how would I have to use this multidimensional array into the following code which uses the DallasTemperature library?

void getTemperature(DeviceAddress deviceAddress)
{
   float tempC = sensors.getTempC(deviceAddress);
   
   if(!(tempC==-127))
   {
     Serial.print("temp : ");
     Serial.print(tempC);  
   }
    else
   {
     Serial.print("Error");
   }
 
}

You want an array of DeviceAddresses. The fact that DeviceAddress is typedefed does not preclude you from creating an array of them.

You populate it, using code like you showed.

There is an example sketch for that library which shows how to discover the devices addresses of the connected sensors. Rather than hard-code the addresses, I'd suggest using that approach to discover the addresses and store them in an array, with a counter to record how many you discovered. Then use the same array and counter to initialise each sensor, and read from it when required.

PeterH:
There is an example sketch for that library which shows how to discover the devices addresses of the connected sensors. Rather than hard-code the addresses, I'd suggest using that approach to discover the addresses and store them in an array, with a counter to record how many you discovered. Then use the same array and counter to initialise each sensor, and read from it when required.

I already tried these examples from the DallasTemperature library but if you need to exchange one of the sensor and you put another one in place then all sensors will be interrogated again and it is possible that all sensors will receive another index. I need to be sure that the sensor which temperature I read out is the correct one and I suppose by using the address it is....
Depending the 8 byte address of the sensor it will come up first in the array of found devices that's why I think that it isn't a good idea to use the index of the library.

Yesterday I tried to make the 2D array as PaulS suggested but I'm struggling with it.......

typedef uint8_t DeviceAddress[8]; ...DeviceAddress is now an array of 8 bytes, correct?

DeviceAddress deviceAddress ... so this is the same as writing Byte deviceAddress[8];

Correct?

I really don't know how to make a 2D array of it....small hint please... :wink:

Thx

small hint please

I already gave you a big one. Forget about the fact there is some typedef lurking in the shadows. Treat DeviceAddress as a type, just like byte, int, char, or float, and create an array of that type.

DeviceAddress adds[8];
DeviceAddress deviceID[7][8]=
  {
    { 0x28, 0xC4, 0xEF, 0xC2, 0x03, 0x00, 0x00, 0x32 },
    { 0x28, 0xAC, 0xF5, 0xC2, 0x03, 0x00, 0x00, 0x91 },
    { 0x28, 0xC1, 0x02, 0xC3, 0x03, 0x00, 0x00, 0x60 },
    { 0x28, 0xC1, 0x19, 0xC3, 0x03, 0x00, 0x00, 0x6C },
    { 0x28, 0x61, 0xE7, 0xC2, 0x03, 0x00, 0x00, 0xBB },
    { 0x28, 0x79, 0x0A, 0xC3, 0x03, 0x00, 0x00, 0xF8 },
    { 0x28, 0xAD, 0x04, 0xC3, 0x03, 0x00, 0x00, 0x42 }
  }

Making the array is one thing, but reading out the sensors one by one by using there unique address is another thing (for me....).
In the code bellow I normally filled in the name of the sensor (deviceAddress) which was(is) typedefed as an array of 8 bytes. But now I don't know how I can send, for example, the first sensor's 8 byte array to the function of the DallasTemperature library?

float tempC = sensors.getTempC(??????)

deviceID is now a 3 dimensional array. Remember that DeviceAddress is equivalent to uint8_t[8].

but reading out the sensors one by one by using there unique address is another thing

It would, of course, be a lot easier if deviceID was declared correctly.

The nth device address, then, would be stored in deviceID[n].

DeviceAddress deviceID[]=
  {
    { 0x28, 0xC4, 0xEF, 0xC2, 0x03, 0x00, 0x00, 0x32 },
    { 0x28, 0xAC, 0xF5, 0xC2, 0x03, 0x00, 0x00, 0x91 },
    { 0x28, 0xC1, 0x02, 0xC3, 0x03, 0x00, 0x00, 0x60 },
    { 0x28, 0xC1, 0x19, 0xC3, 0x03, 0x00, 0x00, 0x6C },
    { 0x28, 0x61, 0xE7, 0xC2, 0x03, 0x00, 0x00, 0xBB },
    { 0x28, 0x79, 0x0A, 0xC3, 0x03, 0x00, 0x00, 0xF8 },
    { 0x28, 0xAD, 0x04, 0xC3, 0x03, 0x00, 0x00, 0x42 }
  }

@PaulS : thanks for the effort.....it finally works and at the end I've learned something...(still a lot to learn... :slight_smile: )

Working code :

//Included libraries
#include <OneWire.h>
#include <DallasTemperature.h>

////////////////////////////////////////////////////////////////////////////////////////////////////
//      Global variables                            /
////////////////////////////////////////////////////////////////////////////////////////////////////

byte resol = 12;
float tempC[7];
////////////////////////////////////////////////////////////////////////////////////////////////////
//      Data wire for temp sensors is plugged into pin 3 on the Arduino                            /
////////////////////////////////////////////////////////////////////////////////////////////////////

#define ONE_WIRE_BUS 3

////////////////////////////////////////////////////////////////////////////////////////////////////
//      Setup a oneWire instance to communicate with any OneWire devices                           /
////////////////////////////////////////////////////////////////////////////////////////////////////

OneWire oneWire(ONE_WIRE_BUS);

////////////////////////////////////////////////////////////////////////////////////////////////////
//      Pass the oneWire references to Dallas Temperature library                                  /
////////////////////////////////////////////////////////////////////////////////////////////////////

DallasTemperature sensors(&oneWire);

////////////////////////////////////////////////////////////////////////////////////////////////////
//      Manually assign sensor addresses                                                           /
////////////////////////////////////////////////////////////////////////////////////////////////////

DeviceAddress deviceID[7]=
  {
    { 0x28, 0xC4, 0xEF, 0xC2, 0x03, 0x00, 0x00, 0x32 },
    { 0x28, 0xAC, 0xF5, 0xC2, 0x03, 0x00, 0x00, 0x91 },
    { 0x28, 0xC1, 0x02, 0xC3, 0x03, 0x00, 0x00, 0x60 },
    { 0x28, 0xC1, 0x19, 0xC3, 0x03, 0x00, 0x00, 0x6C },
    { 0x28, 0x61, 0xE7, 0xC2, 0x03, 0x00, 0x00, 0xBB },
    { 0x28, 0x79, 0x0A, 0xC3, 0x03, 0x00, 0x00, 0xF8 },
    { 0x28, 0xAD, 0x04, 0xC3, 0x03, 0x00, 0x00, 0x42 }
  };

////////////////////////////////////////////////////////////////////////////////////////////////////
//      Setup                                                                                      /
////////////////////////////////////////////////////////////////////////////////////////////////////

void setup(void)
{

  Serial.begin(9600);               // start serial port

  sensors.begin();                  // Start up library
  for (byte i=0; i<=6; i++)
  {
    sensors.setResolution(deviceID[i], resol);
  }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
//      Loop                                                                                        /
/////////////////////////////////////////////////////////////////////////////////////////////////////

void loop(void)
{
  delay(750);
  sensors.requestTemperatures();
  Serial.print("\r\n");
  for (byte i=0; i<=6; i++)
  {
    tempC[i] = sensors.getTempC(deviceID[i]);
    if(!(tempC[i]==-127))
   {
     Serial.print("temp sensor ");
     Serial.print(i);
     Serial.print(":");
     Serial.print(tempC[i]);
     Serial.print(" Celsius");
     Serial.print("\r\n");     
   }
    else
   {
     Serial.print("temp sensor ");
     Serial.print(i);
     Serial.print(":");
     Serial.print("Error");    
   }  
  }
}

Output :

temp sensor 0:22.62 Celsius
temp sensor 1:22.62 Celsius
temp sensor 2:22.50 Celsius
temp sensor 3:22.44 Celsius
temp sensor 4:22.44 Celsius
temp sensor 5:22.44 Celsius
temp sensor 6:22.44 Celsius

Excellent.

DeviceAddress deviceID[7]=

The compiler can count, you know. Let it. Get rid of the 7.

Adding a sensor without the explicit size will simply increase the size of the array. Adding a sensor with the explicit size will require that you change the explicit size, too.

    if(!(tempC[i]==-127))

That take too much study.

    if(tempC[i] != -127)

Says the same thing, in a more readable way.

A little more white space in the output would be a good thing.

PaulS:
Arrays and object oriented programming have as much in common as potato chips and horseradish.

I'm not familiar with that combination. How much DO they have in common?

Are chips in Australia like British chips, or American chips?

PaulS:

DeviceAddress deviceID[7]=

The compiler can count, you know. Let it. Get rid of the 7.

Adding a sensor without the explicit size will simply increase the size of the array. Adding a sensor with the explicit size will require that you change the explicit size, too.

Ok, I see...makes sense. I only did this because most examples of arrays I saw did have the exact size mentioned. In my project not a big difference because there is no need for an extra sensor but I will keep this in mind....

PaulS:

    if(!(tempC[i]==-127))

That take too much study.

    if(tempC[i] != -127)

Says the same thing, in a more readable way.

Much better indeed, the code I used was copied out of another OneWire example.

PaulS:
A little more white space in the output would be a good thing.

In this project there is no need for a Serial print to the serial monitor, it was just for testing the code. The next step of this project is to put all the data into some data string to send it via a RS485 serial communication link to a master unit. At the end the purpose is to send from several modules the temperature data to one unit with a display. I'm planning to use the SimpleModbus library for this.....
So, you will see me again here on this forum I guess.. :slight_smile:

dxw00d:
Are chips in Australia like British chips, or American chips?

If someone offered me "chips", like at a party, I would normally expect the thin pre-cooked ones which are usually sort-of circular in shape.

However "fish and chips" I would normally expect thick-cut potato chips, deep fried, hot.

Then the sort you normally get with hamburgers and places like that, I would call "French Fries" which are thin-cut potato chips, deep fried and (hopefully) hot.

I'm working on something like you. I have 20 sensors, but if I connect more than 7, then I get only zeroes from all. When sensor no 8 and more is disconnected, normal data come. I tried mesured voltage. Normaly Pin 3 and GND is 4,9V, but once on loop (program) it gives 4,5V. When sensor 8 is coneceted, voltage drops to zero for moment. Every sensor woks fine allone. I tried external power supply, but with same result. I use 2 wire connect.

I found problem. My 4k7 pull up resistor was too big. Now I using 1k8 and 19 sensors.