Passing variable name over Serial


hi - after searching around yesterday evening, i'm pretty sure want isn't possible on arduino, i'll ask anyhow - offers different ways.  warning - i'm quite rusty , quite simple predicament!

for purposes of simplified example, let's have 10 leds, , have pins declared (together 2 other variables serial)...

code: [select]

const int led1=2;
const int led2=3;
const int led3=4;
.
.
.
const int led10=11;

string inputstring = "";         // string hold incoming data
boolean stringcomplete = false;  // whether string complete


now, want pass command on serial monitor turns on/off 1 of leds.  i've got serialevent code working type in serial monitor line stored in inputstring variable.  so, might type serial monitor...

code: [select]

led4_on
led8_off


...and stored in inputstring.  process string (and has validation chopped out, example assumes command entered valid...

code: [select]

void loop() {
// process command when newline arrives (handled serialevent script):
  if (stringcomplete) {

      // validation of command in here (all basic stuff)

      string thisled = inputstring.substring(0, inputstring.indexof("_")); // characters before _
      string straction = inputstring.substring(inputstring.indexof("_")+1);  // characters after _
      bool thisaction = true;
      if(straction=="off") {
        thisaction=false;  // assume want on unless says off in string
      }

      // *************************************
      // perform "thisaction" on "thisled"  <---the bit i'm struggling with
      // *************************************

   }
}


at point, i've got name of led want turn off/on stored in thisled , whether want on or off stored in boolean thisaction.  quick test code has 10 if statements saying like...

code: [select]

if(thisled=="led1") {
   digitalwrite(led1, thisaction);
}
if(thisled=="led2") {
   digitalwrite(led2, thisaction);
}
.
.
. etc.


...which bit cumbersome - 10 if statements.  appreciate switch case there, that's still listing leds , performing match.  hoping can done kind of pointer/reference, on couple of lines, i'm having trouble.  after all, i've got name of variable want value stored in "thisled".  i'm thinking goes reasons why "eval" isn't available, fingers crossed.

is possible, or going wrong way?


eventually, planned compiled program hooked sdk of utility.  i'm planning on mapping sdk's variable names display lights variable names led pins in arduino.  can pass through variable name of light has been triggered , arduino have mapped pin.  other way can see if store arduino's pin map in compiled program on pc , pass down pin numbers want turn on , off on serial.

any gratefully appreciated, thank you.

you right in want not possible.  once program compiled loses concept of variable names.

the way doing reasonable small quantities (though i'd stay away string library , use c strings , functions, strcmp()).  larger quantities might want consider couple of arrays (one pin names, 1 pin numbers), or array custom data structure.  iterate through array(s) looking match.


Arduino Forum > Using Arduino > Programming Questions > Passing variable name over Serial


arduino

Comments

Popular posts from this blog

Convierte tu Raspberry en un NAS. Firmware fvdw-sl 15.3 - Raspberry Pi Forums

How to format a Get Request

avrdude: verification error, first mismatch at byte 0x0000 0x0c != 0x62