0

I have a script in Arduino that'll get a letter, and to make things as short a possible I'd like to use a string combined with another variable to get the variable name I need to fill in into the function.

I'd like this because I have a function mySwitch.send(Int, 24). For the first Int variable I need to to send a number that is dependent on the letter I sent, and on the current value of A_stat, I defined these values in variables A_aan, A_uit, B_aan, B_uit, etc.

For example for the letter A I need to fill in the variable A_aan if a_stat == 0. If a_stat == 1 it needs to fill in A_off.

For B I need to fill in the variable B_aan if b_stat == 0, and I need to fill in the variable name B_uit on the place of Int if b_stat == 1.

1 Answer 1

1

Since the value of the variable vary at runtime, the only solution in my opinion is to use some functions.

For instance, if you want to get #_aan, where # is the value in a variable, you can use the following function:

int get_aan(char carat)
{
    switch(carat)
    {
        case 'A':
            return A_aan;
        case 'B':
            return B_aan;
        ...
    }
    return AN_INVALID_VALUE_YOU_DEFINE;
}

Remember to define an invalid value somewhere.

If you have to set that value, you can write a similar set function:

void set_aan(char carat, char value)
{
    switch(carat)
    {
        case 'A':
            A_aan = value;
            break;
        case 'B':
            B_aan = value;
            break;
        ...
    }
}

You can also include some flags. For instance, if you wanted to get variable A_aan when A_stat = 0 and A_off when A_stat != 0, and repeat this for every variable, just modify the first function in:

int get_the_val(char carat)
{
    switch(carat)
    {
        case 'A':
            if (A_stat)
                return A_off;
            else
                return A_aan;
        case 'B':
            if (B_stat)
                return B_off;
            else
                return B_aan;
        ...
    }
    return AN_INVALID_VALUE_YOU_DEFINE;
}
Sign up to request clarification or add additional context in comments.

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.