Using a char to indicate a variable

I know my terminology is wrong, so someone please correct me.

Say myChar = ‘A’ and int A = 13.

How do I do something like digitalWrite(myChar, HIGH) to turn pin 13 high?

Bapstack:
How do I do something like digitalWrite(myChar, HIGH) to turn pin 13 high?

const byte myChar = 13;
void loop() {
    digitalWrite(myChar, HIGH);
}

Bapstack:
I know my terminology is wrong, so someone please correct me.

Say myChar = 'A' and int A = 13.

How do I do something like digitalWrite(myChar, HIGH) to turn pin 13 high?

You have to understand that once the code is compiled the concept of variable names is GONE. They all become just memory addresses. There's no way for something in your code at runtime to refer back to the name of the variable you used when you wrote the program.

But that's why they created pointers. So you could have a variable that "points to" another variable. But by its memory address instead of its name.

If you could give a more concrete example of what you are trying to do, then maybe we could help you see how to do it.

So you CAN do this.

int a = 12;
int b = 13;

int *p;

p = &a;
digitalWrite(*p, HIGH);  // writes pin 12 high because p points to a

p = &b;
digitalWrite(*p, HIGH);  // Now the same line writes 13 high because p now points to b.

I have my function morseInterpreter which returns a character between 'A' and 'Z'. I have 26 const integer variables for pins, A = 2, B = 3, etc.

I want to use the character from my function to indicate which pin I wish to turn on.

Put the pin numbers in an array and use the char to index the array.

byte pinNumbers[] = {1, 2, 3, 4, 5};
// A will be pin 1, B will be pin 2, C will be pin 3 etc.

char which = Serial.read();  // pretend we're getting an 'A', 'B', 'C' etc here.

digitalWrite(pinNumbers[which - 'A'], HIGH);

// subtracting 'A' so that 'A' turns into 0 and 'B' becomes 1 etc.  ( see ascii table if that confuses you)
// and we can use that number as the array index

Which Arduino are you using? The Uno or Nano don't have enough pins to do what you're trying to do.

Delta, thanks for your help. I don't lack the ability to put together some sort of workaround to make my program do what I want. I just wondered if there was any way, using pointers or something, for a character (or string) whose value is the same as a variable's name (String myString = "Bapstack", int Bapstack = 12), to refer to that variable's value. Just trying to boost my coding savvy.

Jack, I'm using a Mega for this.

To code string constants with a value that is the same as its name is easy:

char elephant[] = "elephant";

But I think you should have a two dimensional array to associate characters with pin numbers.

aarg:
To code string constants with a value that is the same as its name is easy:

... But prone to error.
Use the preprocessor's stringify operator and a simple macro for certainty

Okay, just to be completely clear before I move on.

charFunction returns an arbitrary character between 'A' and 'Z'. I have predefined int variables A = 2, B = 3, and so on.

Is there NO WAY for my char value to be interpreted as the name of my variable, short of predefining an array? Again, I'm not looking for workarounds, I just want to know for sure why this isn't possible.

Bapstack:
I just want to know for sure why this isn’t possible.

Once the code is compiled, the variable names no longer exist. All that exists are their memory addresses. Unless you associate the names with the addresses somewhere in your sketch, like in an array, there is no way for the running sketch to find the variable given only a character or string representing a variable name.

You can do such things in many interpreted languages (like Javascript?) but almost never in a compiled language.

What problem is it that you are trying to solve? Even most interpreted languages don’t allow you to use variable names as data. Yet nobody has any problem with it.

It's not a problem. It's just that I want to understand the language better. And if there's a way to do something, I want to know how to do it, or know that it can't be done.

You can associate a char value with an integer by looking at their ASCII values. For example, you want an 'A' to be the value 2, 'B' the value 3, and so on. The ASCII code for the letter 'A' is 65, 'B' is 66, and so on. So, you could write charFunction() as:

int charFunction(char inputChar)
{
    inputChar = toupper(inputChar);
    return inputChar - 63;               // This means 'A' returns 2
}

I'm still not sure if this is what you're trying to do.

Bapstack:
Okay, just to be completely clear before I move on.

charFunction returns an arbitrary character between 'A' and 'Z'. I have predefined int variables A = 2, B = 3, and so on.

Is there NO WAY for my char value to be interpreted as the name of my variable, short of predefining an array? Again, I'm not looking for workarounds, I just want to know for sure why this isn't possible.

How many times do we have to tell you that there is indeed no way to do it.

You have to understand that once your code is compiled and loaded on the Arduino board the variables names no longer exist. The processor on the Arduino is running the hex code generated from your program. It has absolutely NO idea what you used for variable names when you wrote the program. Those names simply do not exist.

How many more ways does that have to be said before you will understand? There are ways to solve your problem. But there is NO WAY for to use a string to refer to a variable in the code running on your Arduino board. Period. Full stop. End of sentence. No ifs ands or buts.