Inputting multiple different variables into the serial monitor

Hi. I would like to define three variables via the serial monitor, they are all integers.

I would like to be able to change them at any time. And have some type of prefix so the Programm knows what integer to change.

For example if I want to define Time A as 500 I want to type "TA500" in the Serial monitor.

I know this is possible but I have no idea how it is done.

I have some bits of code from another project that does that but somehow some of it got lost and I can't finde the article where I copied it from anymore. If you could help me complete the code or get a different solution running it would be very appreciated.

Here it is:

while(Arr[j+2] != 0){

tmp[j] = Arr [j+2];

j++;

}

long int num = constrain(atol(tmp),50,65500);


if(num <= 0){

  Serial.println("Error: Setting Run Time to 100 ms");

  return 100;

}

return num;

}

void serialFlush(){ //Flushes Serial Buffer

while(Serial.available()) {

char t = Serial.read();

}

}

void dispenserFun(char Arr[], int len){ //Converting Input Stream into possible Functions

if(Arr[0] == 'T' && Arr[1] == 'A'){

  zeit[0] = Char2Int(Arr, len);

  Serial.print("Time 1: ");

  Serial.print(time[0]);

  Serial.println(" ms");

}

else if(Arr[0] == 'T' && Arr[1] == 'B'){

  zeit[1] = Char2Int(Arr, len);

  Serial.print("time 2");

  Serial.print(time[1]);

  Serial.println(" ms");

}

else if(Arr[0] == 'D' && Arr[1] == 'A'){

  //Dispense

  Serial.print("Running for ");

  Serial.print(time[0]);

  Serial.println(" ms");

}

else if(Arr[0] == 'D' && Arr[1] == 'B'){

  Serial.print("Running for ");

  Serial.print(time[1]);

  Serial.println(" ms");

}

else if(strcmp(Arr,"\r\n")==0){

  //Do nothing

}

else if(strcmp(Arr,"INFO\r\n")==0){

//INFO like: Serial.print("Possible tines 1:50-500 2:50-100");

}

else{

  Serial.println("No command");

}

}

void setup() {

Serial.begin(9600);

//INFO like: Serial.print("Possible tines 1:50-500 2:50-100");

}

void loop() {

if(Serial.available()){

while(Serial.available() && strPointer < 50){ //reading out input stream

  recData[strPointer] = Serial.read();

  if ((recData[strPointer]>='a') && (recData[strPointer]<='z')) recData[strPointer]+='A'-'a';  //Kapital latters

  strPointer++;

}

recData[strPointer]=0;

dispenserFun(recData, strPointer);

serialFlush();

strPointer = 0;

}

delay(1000);

}

Welcome, but...

Do you see what a mess that made?

Please read the forum intro thread for instructions on how to post code.
https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966#use-code-tags

Add the received characters to a char array. When you receive the end of input (CR), convert to an integer using atoi().

char input[10];
uint8_t cnt = 0;


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

void loop()
{
  if (Serial.available() > 0)
  {
    char c = Serial.read();

    if (c == '\r')
    {
      input[cnt] = '\0';

      uint16_t num = atoi(input);
      
      Serial.print("Integer is ");
      Serial.println(num);
      
      cnt = 0;
    }
    else if (c >= '0' && c <= '9')
      input[cnt++] = c; 
  }
}

consider

list
           0 varA
           0 varB
           0 varC
varA 123
list
         123 varA
           0 varB
           0 varC
varB 888
varC 7123
list
         123 varA
         888 varB
        7123 varC

int varA;
int varB;
int varC;

char s [60];

// -----------------------------------------------------------------------------
char buf [40];
char lbl [10];
int  var;

void
loop (void)
{
    if (Serial.available ())  {
        int n = Serial.readBytesUntil ('\n', buf, sizeof(buf)-1);
        buf [n] = '\0';

        Serial.println (buf);
        sscanf (buf, "%s %d", lbl, &var);


        if (! strcmp ("varA", lbl))
            varA = var;
        else if (! strcmp ("varB", lbl))
            varB = var;
        else if (! strcmp ("varC", lbl))
            varC = var;
        else if (! strcmp ("list", lbl))  {
            sprintf (s, "      %6d varA", varA);
            Serial.println (s);
            sprintf (s, "      %6d varB", varB);
            Serial.println (s);
            sprintf (s, "      %6d varC", varC);
            Serial.println (s);
        }
    }
}

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

Time 500 sounds like a delay of 500 milliseconds.
Well if you want to able to enter new time-values at any time your whole code must be based on non-blocking timing.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.