0

Hello in a previous question i was able to set variables through if statements

  const uint16_t inputLength = 512;
int ss = 0;
int bHold = 0;

void setup() { 
  Serial.begin(9600);
 }

 void loop() {
  if ( Serial.available() > 0 )
  {
    static char input[inputLength];
    static uint16_t i;
    char c = Serial.read();

    if ( c != '\r' && c != '\n' && i < inputLength - 1)
      input[i++] = c;

    else
    {
      input[i] = '\0';
      i = 0;

      uint16_t array[80];
      uint16_t j = 0;

      if ( !strncmp(input, "ss", 2) )
      {        
        char* p = input + 2;
        while ( (p = strchr(p, ' ')) != NULL )
          array[j++] = strtol(p, &p, 10); //

        for ( uint8_t i = 0; i < j; i++ )
        {
          ss = array[i];
          Serial.println(ss);
        }
      }

      if ( !strncmp(input, "bHold", 5) )
      {        
        char* p = input + 5;
        while ( (p = strchr(p, ' ')) != NULL )
          array[j++] = strtol(p, &p, 10);  

        for ( uint8_t i = 0; i < j; i++ )
        {
         bHold = array[i];                   
         Serial.println(bHold);
        }
      }

      if ( !strncmp(input, "Whos There", 10) )
      {
        Serial.print("me");
      }
      if ( !strncmp(input, "hello", 5) )
      {
        Serial.print("hi");
      }
      if ( !strncmp(input, "sa", 2) )
      {
        Serial.print("  ss= ");
        Serial.print(ss);

        Serial.print("  bHold= ");
        Serial.print(bHold);

        Serial.println(" ");
        Serial.println("==============================");
      }
    }
  }
}

if works! but if i keep adding variables i have to keep adding if statements and this will get longer. so then i went with code like this that allows me to set all variables at once

#define INPUT_SIZE 30

void setup()
{
  Serial.begin(9600);

}
void loop()
{

  while (Serial.available() > 0) {


    int _mode = Serial.parseInt();
    int _ss = Serial.parseInt();
    int _bHold = Serial.parseInt();

    if (Serial.read() == '\n') {
      ss = _ss;
      bHold = _bHold;
      mode = _mode;

    }
  }

}

this works awesome! but i have to set all variables at once. my question is is there a way i can simply input a command like ss = 10 press enter then bHold = 30 and not have to input any other variable? thanks i advance. please point me in right direction Maybe make a function?

1
0

Somewhere along the line you have to tell your program how to map names to actual variables. Once you have compiled your program it forgets the names of the variables - those only exist in the source code.

So you have to have some other manual list of those variable names - be that in the form of if(...) statements, or an array of structs with the name in them, or whatever.

There's no getting away from it - you need that manual mapping in some form or other.

The closest you could get would be to set up some kind of hash system using structs and linked lists, whereby you could set any key/value pair, but then of course your program would still have to know to look for certain keys in the hash to do things with them, so that would be pretty pointless since you've just moved the variable mapping from one place to another.

0

You can store variables on the fly in a simple array or linked list. A linked list would probably be better, but for now we'll just use an array.

const int MAX_VARS = 10;
const int NAME_SIZE = 16;

typedef _var
{
    char  name[NAME_SIZE];
    int   value;
} Var;


Var variables[MAX_VARS];
unsigned int var_count = 0;

void loop()
{
    char *command = getSerialInput(); // get a line of input from the serial stream

    // parse the command "set varname=value"
    if (strncmp(command, "set ", 4) == 0)
    {
        char *new_name = command + 4; // skip the 'set '

        char *ptr = strchr(command,'='); // find the equals character
        if (ptr != NULL)
        {
             *ptr = '\0';              // Break the string in half about the '='
             char *new_value = ptr+1;  // Everything after the equals is the value

             // Now we have a parsed variable name, and value
             // Have we already stored this variable?
             int i;
             for (i=0; i<var_count; i++)
             {
                 if (strcmp(variables[i].name, new_name) == 0)
                 {
                     // Found an existing copy - update it
                     variables[i].value = new_value;
                     break;
                 }
             }

             // Did we find the variable already?  No - add a new one
             if (i == var_count && i< MAX_VARS)
             {
                 strncpy(variables[i].name, new_name, NAME_SIZE);
                 variables[i].name[NAME_SIZE-1] = '\0'; // fix any strncpy() overflow
                 variables[i].value = new_value;
                 var_count += 1;
             }
             else if (i == MAX_VARS)
             {
                 Serial.println("Out of variable slots");
             }
        }
    }
    // parse the command "get varname"
    else if (strncmp(command, "get ", 4) == 0)
    {
         char *new_name = command+4;
         int i;
         for (i=0; i<var_count; i++)
         {
             if (strcmp(variables[i].name, new_name) == 0)
             {
                 // Found an existing copy - output it
                 Serial.println(value, DEC);
                 break;
             }
         }

         // Did we find the variable already?  No - error
         if (i == var_count)
         {
              Serial.print(new_name);
              Serial.println(" - not found");
         }
    }
}
else
{
    Serial.println("HELP: \"set <name>=<value>\" or \"get <name>\"");
}

This would be better with dynamically allocated memory, so the programme only uses exactly what it needs. But I wanted to show a way of storing a list of user input without complications. Obviously this programme omits a lot of error checking too - like when you run out of array slots in variables; or when the user input is bad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.