Pages: [1]   Go Down
Author Topic: Is there a way to split serial data into 3 ints?  (Read 2019 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
God Member
*****
Karma: 13
Posts: 905
Twitter: @simonmonk2
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

--
My New Arduino Book: http://www.arduinobook.com

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
God Member
*****
Karma: 13
Posts: 905
Twitter: @simonmonk2
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Serial.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.
Logged

--
My New Arduino Book: http://www.arduinobook.com

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26621
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Always remember to to use Serial.available to make sure there is something to read before calling Serial.read.
Logged

"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.

Pages: [1]   Go Up
Jump to: