I want to handle 10 temperature sensors (type DS18B20). Therefore I initially read all addresses of the sensors. Each address consists of 8 bytes. I can successfully read all addresses of all sensors. One address can be stored in a byte-array, i.e.:
byte SensorAddr[8];
For easy handling in different functions I would like to store all available sensor-addresses in an array, i.e.:
uint8_t AllSensorsAddr[10][8];
My question:
How can I store all 8 bytes at one time in the array?
Something like
while(DS1820.search(SensorAddr)) {
if ((SensorAddr[0] == 0x10) or (SensorAddr[0] == 0x22) or (SensorAddr[0] == 0x28)) {
AllSensorsAddr[SensorCtr] = SensorAddr;
showAddress(SensorCtr, SensorAddr);
SensorCtr++;
}
}
There are no compound assignments of base types in C++. You are trying to copy a vector of values with one assignment statement (because one dimension of a 2 dimensional array is itself an array, not a single value).
You have to copy the values one by one, or use some function that does that. You could create a Sensor Address class, and overload the '=' operator to perform that function.
"Easy handling" of arrays by different functions is usually done by somehow managing to keep the data in one place and pass pointers to the data to those functions, instead. I don't know enough about your program to suggest how you might do that.
aarg:
There are no compound assignments of base types in C++. You are trying to copy a vector of values with one assignment statement (because one dimension of a 2 dimensional array is itself an array, not a single value).
Not if you use C-style arrays, but you can simply copy C++ std::arrays without any issues:
This won't work on AVR Arduinos, because they don't ship with the C++ standard library out of the box. It will work on ARM boards and ESP32/8266 boards.
That being said, it's much easier to just store the addresses in your array directly:
aarg:
You have to copy the values one by one, or use some function that does that. You could create a Sensor Address class, and overload the '=' operator to perform that function.
There's no need to overload the assignment operator, the default one that's generated by the compiler will do a deep copy of the array members for you.
Thanks @aarg and @Robin2 for the different hints and tipps. I will have a more close look to the mentioned methods in order to learn more about c++
For my sensor-task I have choosen the method from @PieterP to directly assign the address in the read-command.
while (count < num_addresses && ow.search(addresses[count])) {
count++;
}
There's no need to overload the assignment operator, the default one that's generated by the compiler will do a deep copy of the array members for you.
Pieter
In my opinion it´s the most direct way to solve the issue.