Hi All,
I'm trying to find a way to simplify the code that I have for reading multiple sensors, I'm not sure if what I'm trying to achieve can be done... or if I'm doing it the right way...
I have a variable (sensorsUsed) which I want to use to control a loop which can be set from any value between 0 (one sensor) or 9 (ten sensors)
I can do it through multiple if statements but I want to simplify the code somewhat making it easier to change at a later date, what I'm thinking is something like this: (my C skill is limited!!)
int sensorsUsed = 5;
void setup() {
if (enableSerialMon){
Serial.begin(19200); // enable serial monitor if set in config
}
}
void loop(){
int sensor[sensorsUsed]{};
int thermocouple[sensorsUsed]{};
int i = 0;
for (int i = 0; i < sensorsUsed; i++){
sensor[i] = thermocouple[i].readCelsius();
Serial.print("Sensor ");
Serial.print([i]);
Serial.print(" value is ");
Serial.println(sensor[i]);
delay(1000);
}
}
now this comes up with an error that I don't understand -
request for member 'readCelsius' in 'thermocouple[i]', which is of non-class type 'int'
I just want sensor0 to be loaded with the read from thermocouple0.readCelsius()
then sensor1 to be loaded with the read from thermocouple1.readCelsius()
then sensor2 to be loaded with the read from thermocouple2.readCelsius()
etc.
All defined by the 'sensorsUsed' variable, the rest is just debugging to see if it is working or not.
Do I have the wrong understanding of arrays/loops/programming in general?? I can't help but thinking this is the right way to do it but not sure why it won't work...