Getting Variable Name From Another Variable

I pass serial commands to Ardiuno in the following format PARM1/PARM2/PARM3 which is being split by ardiuno correctly, the value of parm2 will have a name for another var which stores pin info.

for Eg: if I pass the Serial command STW/LED/0
I made a code but it doent compile

new_ard.ino: In function ‘void loop()’:
new_ard:39: error: invalid conversion from ‘char*’ to ‘char’
new_ard:39: error: initializing argument 1 of ‘void status(char)’

int LED=13;

void setup() {

void status(char name) {
int status=digitalRead(name);

void loop () {
char cmd[15];
int i=0;

if (Serial.available()>0) {
  for (i=0;i<15;i++) {

  char* fxn=strtok(cmd,"/\0");
  char* obj=strtok(NULL,"/\0");
  int state=atoi(strtok(NULL,"/\0"));

  if (strcmp (fxn,"STW") == 0) {
  else if (strcmp (fxn,"WL") == 0) {
  Serial.println("Invalid Command");


You should take a look at the atoi() function.

It does not work with atoi

int status=digitalRead(atoi(name));

What kind of data do you send over the serial interface? Is it really '\0' terminated? Is it ASCII or binary data? Are your examples literal or are you using placeholders there?

Your status() function expects a character parameter but you're giving it a character pointer. That's why the compiler is throwing an error.

What does "It does not work with atoi" mean? Do you get a compiler error? Does it return a value you didn't expect?

Assuming you have received the three parameters and stored them as strings, you could use strcmp to compare the second parameter value against each of your supported variable names until you find a match:

// assume the value of PARM2 is stored in varName and the value of PARM3 is stored in varValue
if(strcmp(varName, "LED") == 0)
   led = atoi(varValue);
else if(strcmp(varName, "foo") == 0)
   ... etc

let me explain in simpler way.

char name[]="LED";
int LED=13;
int WTO=31;

int status=digitalRead(name);

now if the value of name is LED it should call pin 13 and if the value is WTO it should call pin 31,

As the name variable value will contain the name of the pin varible, i lont want to if and if else as this will be a long chain

The C language simply doesn't work like this. You will have to go about the task another way. This is because C is compiled, once it is all variable names are lost and replaced by addresses. So if you want to pass a string and have that string call up the value of a variable then you will have to look at each possibility in turn.

i lont want to if and if else as this will be a long chain

Whatever approach you take, you will have to explicitly check the name for each supported variable and there is no way to avoid that.

Using if/else to check each name in turn is not the most compact way to implement it, but it’s the simplest. If you only have a few dozen names to check, just bite the bullet and type them in. If you have more than that, I’d suggest maybe this approach isn’t a sensible way to solve the problem and you should look for possible alternatives. (Since I don’t know what you’re trying to achieve, I can’t advise you what alternatives would be suitable.)

you can make 2 array’s, one containing the names (char) and the other containing pointers to the variable associated with that name

something like this should get you started.

char [3][5] names = {"LED1", "LED2", "LED3"};
int [3] pins = { 12, 13, 7 };

for (int i=0; i< 3; i++)
  if (strcmp(instr, names[i]) == 0) digitalwrite(pins[i], paramX);