Serial.read String and split it into three integers

Hello everyone,

I read a lot of threads about reading strings from the serial port but most of them doesn't work for me. What I want is simply receive a String like "R:200;G:100;B:92" over the Serial.read method and split the data into three integers in order to pass them to my RGB Led Stripe to change the color. The stirng comes from my java web application, so I could change the string if this is not useful. I could also send "200;100;92" but the problem still remains the same.

Can somebody help me out there? Normally I post the code I have up to now, but that wouldn't help at all because its not working closely.

Best regards and many greetings, Hauke

Serial.read() returns a single character at a time...

You don't so much need to split them up as combine them, and remember which number you are combining them into. Rather than "R:200;G:100;B:92", If you sent "200R100G92B" the colour codes follow the number, which simplifies the logic a bit. The following code might (should but not compiled) work.

int red, blue, green;
int work = 0;

void loop(void)
{
char ch = 0 ;
if (Serial.available())
   {
   // there is a character ready to be read
       ch = Serial.read() ; // read it
        if (isdigit(ch)) // 0 to 9
             work = work*10 + ch - '0' ; // build up number from string of digits 
        else
        {
            if (ch == 'R') red = work ;
            else if (ch == 'G') green = work ;
            else if (ch == 'B') blue = work ;
            work = 0 ; // reset work for next number
        }
   }
if (ch == 'B') // just got last colour
   {
      // do something with red green blue values
   }
}

I would use strtok() function

http://www.elook.org/programming/c/strtok.html

also i would do 200:100:92 because the order of RGB is common sense. you don't really need to tell yourself what the order is if you're the one who made the order.

or do it like dafid said.

If you search the post titles for serial going back a couple of days, you will probably find some example code I've posted that might be useful.