Setting multiple variables via a Serial.readString();

Hello , :slight_smile:

For a project that I am creating I want to set multiple variables to control a ledstrip via Serial.readString();
e.g.

int aStrip1AmountOn=5;
int bStrip2AmountOn=5;
int cStrip3AmountOn=5;

String incoming;

Now I want to read the serial via

incoming = Serial.readString();

Then I want to send a string over the serial from my computer for example: A10. This means that I want to set strip aStrip1AmountOn=10; and if I send C3 it sets cStrip3AmountOn=5 to three.

I don't exactly know how I can do this set this. I hope you understand what I mean and that someone can help me :slight_smile:

Thanks a lot in advance!

Niek

if I send C3 it sets cStrip3AmountOn=5 to three.

If you send C3, why would you assign 5 to cStrip3AmountOn?

I don't exactly know how I can do this set this.

If you never plan to have more than 26 "names", then the variable to be set is easily determined without the need to add delimiters.

Converting the 2nd and remaining characters to an int is pretty simple.

String uselessWasteOfResources = Serial.readString();

if(uselessWasteOfResources.length() > 0)
{
   if(uselessWasteOfResources[0] == 'A')
   {
      uselessWasteOfResources[0] = ' ';
      aStrip1AmountOn = uselessWasteOfResources.toInt();
   }
   else if(uselessWasteOfResources[0] == 'B')
   {
      uselessWasteOfResources[0] = ' ';
      bStrip1AmountOn = uselessWasteOfResources.toInt();
   }
   // more else if statements
}

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

And if you need more help please post your complete program.

...R