Go Down

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

#### P8ntBal1551

##### Jan 21, 2011, 08:56 am
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.

#### Si

#1
##### Jan 21, 2011, 09:35 am
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

#### P8ntBal1551

#2
##### Jan 21, 2011, 10:41 am
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.

#### Si

#3
##### Jan 21, 2011, 10:52 am
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 digitsSerial.read();Serial.read();Serial.read();// 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

#### AWOL

#4
##### Jan 21, 2011, 11:03 am
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.

Go Up

Please enter a valid email to subscribe