I've occured problem looping through array of structs.
I've got this struct:
struct sensor
{
float data = 0;
bool status = false;
Idx idx; //enum
SensorType type; //enum
};
It contains some sensors for my arduino.
So to send data, that I've recieved from them I want to use array of sensors in loop.
This is my array definition:
sensor sensors[] = {light, pressure, pressureHpa, temperature, humidity, temperatureOut, humidityOut};
Finally, I want to loop through this array and call some methods, but when I try to access any sensor by index I get zero values for all properties.
for (unsigned int i = 0; i < sizeof(sensors) / sizeof(sensor); i++)
{
Serial.print("type:");
Serial.print(sensors[i].type);
Serial.print(" sens.idx:");
Serial.print(sensors[i].idx);
Serial.print(" sens.data:");
Serial.print(sensors[i].data);
Serial.print(" sens.status:");
Serial.print(sensors[i].status);
Serial.println();
}
In this loop I've got output like:
type:0 sens.idx:0 sens.data:0.00 sens.status:0
type:0 sens.idx:0 sens.data:0.00 sens.status:0
type:0 sens.idx:0 sens.data:0.00 sens.status:0
type:0 sens.idx:0 sens.data:0.00 sens.status:0
type:0 sens.idx:0 sens.data:0.00 sens.status:0
type:0 sens.idx:0 sens.data:0.00 sens.status:0
type:0 sens.idx:0 sens.data:0.00 sens.status:0
But, when I access any of my structs directly like:
Serial.print("type:");
Serial.print(pressure.type);
Serial.print(" pressure.idx:");
Serial.print(pressure.idx);
Serial.print(" pressure.data:");
Serial.print(pressure.data);
Serial.print(" sens.status:");
Serial.print(pressure.status);
Serial.println();
I get:
type:3 pressure.idx:13 pressure.data:0.00 sens.status:1