Go Down

Topic: Is there a way to split serial data into 3 ints? (Read 3730 times) previous topic - next topic


I am trying to send an RGB value from processing to an arduino, i found the tutorial showing from arduino to processing, but that requires the arduino to have the split method, which it doesn't. I really don't know where to start on this, any help would be appreciated. Thank you in advanced.
P.S. the serial data is coming in "RRR.R,GGG.G,BBB.B" form. Thank you again.


If its always fixed length messages, then you can just read each character and put it in the appropriate colour string array as you read it.

But, I'm not sure this is what you need. Are you planning to convert each of the RGB values into a number?

If so, into an int 0-255 for the PWM output by any chance?

Give us a bit more detail.
I write books about Arduino and Electronics: http://simonmonk.org


Yes, actually, I am going to be using an RGB LED.

I'm going to use a color picker to send values for the mouse point, it will send them in sets of three, the RGB value which will be a number between 0.0 and 255.0. It will send the three values with a comma in between.

On the arduino i want it to take these values and split them on the comma and then interpret the first value as the PWM brightness of pin 5, second as pin 6 and third as pin 9.


OK, first thing is, you need whole numbers (ints) not floats for the PWM outputs, so there is little point in keeping the bit after the '.' and I wouldn't even botehr rounding it, because the colors are just not going to be that accurate.

I don't really have time to write the C for you, but the pseudo code I'd use is as follows.
Code: [Select]

int r = 0;

char ch1 = Serial.read();
char ch2 = Serial.read();
char ch3 = Serial.read();

r = (ch1 - '0') * 100 + (ch2 - '0') * 10 + (ch3 - '0');
Serial.read(); // skip over unwanted characters to get to the right place for the 3 green digits

// repeat for green and blue.

Anyway, if you get the idea and get it working long hand, then you can always optimise things into loops if you want.
I write books about Arduino and Electronics: http://simonmonk.org


Always remember to to use Serial.available to make sure there is something to read before calling Serial.read.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Go Up