There's already a question about adressing variables with strings in Arduino, but the answers given didn't apply to my problem.
I have multiple sensors (around 14 and the number might increase) connected to my Arduino, I also have relays, engines and RFIDs. I'm creating a function that checks if all of the sensors are active.
The idea is basically this:
#define Sensor_1 2
#define Sensor_2 3
#define Sensor_3 4
#define Sensor_4 5
#define Sensor_5 6
int checkSensors(){
int all_active = 0;
int num_sens = 5;
int n;
int active_sens = 0;
for(n= 1; n <= num_sens; n++) {
if( !digitalRead("Sensor_" + n)) {
active_sens= active_sens+ 1;
}
else {
all_active = 0;
return ( all_active);
}
}
if(active_sens== num_sens) {
all_active = 1;
return(all_active);
}
}
The problem is: I want to address the variable Sensor_n, but I can't find out a way to do it. The error message I get is referring to the digitalRead("Sensor_" + n ) command.
error: invalid conversion from 'const char*' to 'uint8_t {aka unsigned char}' [-fpermissive]
I already tried to use "Sensor_" in a String = "Sensor_", I've tried to force a typecast to uint8_t, but the error message says that it loses precision.
I also tried the .toCharArray command, but it failed as well.
Is there a way to access a variable through a string + int?
I'm more familiar with the "loose" variables in PHP, so this is giving me a lot of trouble.
enumvalues into astd::mapor other type of container (even if it's a static array) then to afindto retrieve the value based on the string.