Hi All,
In my project I need to define a struct to carry data regarding some "channel". I want to store 4 pin numbers for the channel and some additional info. Then I want to have an array of channels. In my first prototype I'll use Pro Mini which can have up to 4 channels. I plan do have variable number of channels so array of channel structs seems to be best for me.
My issue comes when I change fields/properties of my struct in loop(). Next time loop() is called the modified member gets reset to original value which I put there in setup(). I feel like I need to use pointers, I tried but it didn't worked. Here's my code:
#define CHANNEL_NUMBER 4
#define START_PIN 2
struct ChannelStruct
{
byte id;
byte measurePin;
byte load1Pin;
byte load2Pin;
byte finishPin;
byte lastVoltageIndex;
};
typedef struct ChannelStruct Channel;
Channel channels[CHANNEL_NUMBER];
void setup()
{
Serial.begin(9600);
Serial.println("setup");
for (byte i=0; i < CHANNEL_NUMBER; i++)
{
byte channelStartPin = START_PIN + i * 3;
Channel c;
c.id = i;
c.measurePin = i;
c.load1Pin = channelStartPin;
c.load2Pin = channelStartPin + 1;
c.finishPin = channelStartPin + 2;
c.lastVoltageIndex = 255;
channels[i] = c;
Serial.print("Channel ID: "); Serial.println(i);
Serial.print("Address: "); Serial.println((int) &c);
}
}
void loop()
{
Serial.println("loop");
for (byte i=0; i < CHANNEL_NUMBER; i++)
{
Channel c = channels[i];
if (c.lastVoltageIndex == 255)
{
c.lastVoltageIndex = 0;
Serial.print("Channel ID: "); Serial.println(c.id);
Serial.println("Init");
}
}
delay(1000);
}
In setup() I put lastVoltageIndex = 255 which I use as a flag that this channel is not initialized. In the loop I'm checking if lastVoltageIndex == 255 and if so I want to do some init (not included here) and update struct field to 0 so that init will never happen again. What happens is Init is called every time because lastVoltageIndex is always 255.
Please advice. I went through reference and through all struct related threads on the forum. I'm not good at C++. I'm using C# for 10+ years but it does not help now