0

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.

1
  • 2
    C++ is not PHP. You'll likely want to stuff the names and associated enum values into a std::map or other type of container (even if it's a static array) then to a find to retrieve the value based on the string. Commented Apr 27, 2015 at 19:41

2 Answers 2

2

There are a few issues with your code. First you can't get the value of a variable or a define by dynamically using a string that is the name of the variable. It doesn't work that way in C. The easiest approach is to use an array and then just index through it. To make this work well I've changed the for loop to count from 0 since the array is indexed starting at 0. I changed the all_active logic assuming that at some point later you are going to want to know how many sensors are active instead of just whether they are all active or not. If you don't want that, then your logic is also more complicated than needed. It could just return 1 at the end of the for loop since all must have passed the test to get there.

#define Sensor_1 2
#define Sensor_2 3
#define Sensor_3 4
#define Sensor_4 5
#define Sensor_5 6
int sensors[] = {Sensor_1, Sensor_2, Sensor_3, Sensor_4, Sensor_5};

int checkSensors(){
    int all_active = 1;
    int num_sens = 5; 
    int n;
    int active_sens = 0; 

    for(n= 0; n < num_sens; n++){
        if( !digitalRead(sensors[n])){
            active_sens= active_sens+ 1;    
        }
        else {
            all_active = 0;
        }
     }
     return all_active;
}
Sign up to request clarification or add additional context in comments.

Comments

1

In C this line will not work

if( !digitalRead("Sensor_" + n))

you cannot build a string like this in C. Since you didn't post the function digitalRead() I presume it takes a char* type, here a string, which in C you could build like this

char senstr[50];
sprintf(senstr, "Sensor_%d", n);
...
if (!digitalRead(senstr)) { ...

As a side issue, please get used to iterating loops from 0. You add 1 to interface with humans.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.