UART strings with values for LED control

Hi all,

I have successfully interfaced my digital LED stripes, and at the phase where i need to control them through UART.
I am trying to figure out a way where i can send strings with functions followed by parameters or simply just color values to begin with, in following fashion:

"ledbreath 254,235,232" or "ledblink 0,235,0"

Unfortunately this kind of parsing usually ends up in nasty spaghetti code, so i was hoping perhaps anybody had any experience on how to handle this particular technique?

Thanks in advance.

Unfortunately this kind of parsing usually ends up in nasty spaghetti code

That never happens to me. Sometimes I get ravioli or porkchops, but I've never gotten spaghetti.

What have you tried? Parsing the strings you show is very straightforward, IF they are strings.

PaulS:

Unfortunately this kind of parsing usually ends up in nasty spaghetti code

That never happens to me. Sometimes I get ravioli or porkchops, but I've never gotten spaghetti.

What have you tried? Parsing the strings you show is very straightforward, IF they are strings.

Nice to hear from a pasta lover :slight_smile:

What i have done so far is sending following sequence:
"ledblink\n"
"234\n"
"220\n"
"234\n\r"

Which i have implemented in a statemachine, not so elegant, but quite quick and dirty. I would rather send "ledblink 234,0,23\n" in one sequence and parse this.

I would rather send "ledblink 234,0,23\n" in one sequence and parse this.

So, do it. Collect the data in a string (a NULL terminated char array), and then use strtok() to parse it, and atoi() to convert (some of) the tokens to numbers.

PaulS:

I would rather send "ledblink 234,0,23\n" in one sequence and parse this.

So, do it. Collect the data in a string (a NULL terminated char array), and then use strtok() to parse it, and atoi() to convert (some of) the tokens to numbers.

I fell over this example you have made: Arduino 2560R3: Receiving a stream of data over UART - #7 by system - Networking, Protocols, and Devices - Arduino Forum

But is there a chance that i can tokenize the char array you accumulate all the data into?

  char * pch;
  pch = strtok (inData,"< ,>");
  while (pch != NULL)
  {
    Serial.println(pch);
    pch = strtok (NULL, "< ,>");
  }

Did the trick i assume..

Did the trick i assume..

Didn't you test?

There are no < or > in the inData array, so they will never trigger the end of a token. Not much point of including them.