Convert strings to inputs and outputs names?

I know its a little bit strange but this is what I need to do.
The code gets some string (*char or String), in the name "A0" or "A1"

Then I need to use this string to read the relevant analog - A0 or A1 accordingly.
so I can't just use the string as the pin name, and the only way I see is to use some conditions:

//pseudo
  if( A0)
      read A0
  else if (A1)
      read A1

Is there any other elegant way to do this directly using the strings names with some conversion ?

No, there is no direct way.

A way would be to

  • Read the full string. A delimiter like newline can assure you read it all
  • Split it into the right category. If you only want to do analog (aka start with A) then you're done
  • Convert the number part from ASCII to a value. (hint, atoi() )
  • Use that value to do the reading analogRead(0) will read A0 etc

Btw, just go for the char array version.

Since you are only interested in analog names, you could do something like (haven't tested it):

  char Name[2];
  analogRead(int(Name[1]) - int('0'));

Obviously you need to ensure that what you read is actually the correct name of an analog pin.

Since a char is an int, just:

 char Name[3];  // leave space for null
 ...    // populate the string
 if (Name[1] >= '0' && Name[1] <= '5')  // be paranoid
   value = analogRead(Name[1]-'0');

gil22:
I know its a little bit strange but this is what I need to do.
The code gets some string (*char or String), in the name "A0" or "A1"

Then I need to use this string to read the relevant analog - A0 or A1 accordingly.
so I can't just use the string as the pin name, and the only way I see is to use some conditions:

//pseudo

if( A0)
      read A0
  else if (A1)
      read A1





Is there any other elegant way to do this directly using the strings names with some conversion ?

Encapsulate it in a function.

val = analogRead(strToPort(inputString));