c - How to take a variable input and attach it to another variable? -


i'm having difficulty finding answer this, because don't know how phrase question simple google query. here's i'm trying do:

a user inputs number , digit attached end of variable name (see @ bottom).

int enablepin1 =1; int revpin1 = 2;  int fwdpin1 =3;  int enablepin2 = 4;  int revpin = 5;  int fwdpin2 = 6;  int enablepin3 = 7;  int revpin3 = 8;  int fwdpin3 = 9;  int ch = serial.read();  if (isdigit(ch)){ int selection = (ch - '0');   setmotor (selection, 255, 0); }  //user inputs motor number between 1 -3. below, 255 , 0 speed & direction //for example (2 , 255, 0)  //////////////////////here's need help... //////////i want each (motorselection) variable replaced whatever   user inputs, in case, 2, enablepin2, revpin2, fwdpin2.   void setmotor1(int motorselection, int speed, boolean reverse) {   analogwrite(enablepin(motorselection), speed);   digitalwrite(revpin(motorselection), ! reverse);   digitalwrite(fwdpin(motorselection), reverse); } 

it not possible described. however, can achieve larger goal using arrays instead of numbered variables:

static const int enablepin[3] = { 1, 4, 7 }; static const int revpin[3]    = { 2, 5, 8 }; static const int fwdpin[3]    = { 3, 6, 9 };  // ... int ch = serial.read(); if (ch >= '0' && ch <= '3') {     int selection = ch - '0';      analogwrite(enablepin[selection], speed);     digitalwrite(revpin[selection],   !reverse);     digitalwrite(fwdpin[selection],   reverse); } 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -