I have a variable that has a pin number assigned to it . Later in the program I will receive that variable's name as a string.
My problem is how how can I use tha string as the variable name.
int led = 1;
String light;
void setup(){
pinMode(led ,OUTPUT);
}
void loop(){
//this is the strong that I will receive
light = led;
digitalWrite(light, HIGH);
//for sure this function won't work because two intgers
//as parameter not int and string
// and this is my problem: how can I deal with the content
// of light (led) as the variable name not a string
}
Seif_1999:
Later in the program I will receive that variable's name as a string.
You can do that sort of thing with an interpreted language like Python but not with a compiled language such as C++. In an Arduino the variable names are converted by the compiler into memory addresses. The variable names do not exist at runtime.
So you receive the string "led", store the string into the String variable 'light' and want to use the value of the variable 'led' when the String variable 'light' contains the value "led"?
if (light == "led")
digitalWrite(led, HIGH);
else if (light == "otherVariable")
digitalWrite(otherVariable, HIGH);
else if (light == "thirdVariable")
digitalWrite(thirdVariable, HIGH);
else
Serial.println("No matching variable name.");
If you have more than three variables with names you should instead store the values in an array and have a parallel array of names.