I know this has been asked before, but I'm new to Arduino and Processing, and I'm trying to take this step by step.
I would like to pass an RGB value from processing, into Arduino via the Serial.Write/Read.
I have my Processing as:
void draw(){
//orange colour in RGB values
color _rgbColor = color(255, 40, 0);
//convert colour into hex
String _hexColor = hex(_rgbColor, 6);
//convert colour into binary
String _binaryColor = binary(_rgbColor);
//pass colour to port
myPort.write(_hexColor);
delay(500);
}
I assume I can just pass the HEX value straight across, right?
First problem I get is that I can't debug what my Arduino device is getting (when I run the Processing program, the Serial Monitor wont work. Seems like I can only use one or the other). I can print the myPort.read within my Processing after the delay, and I get:
-1
So I'm not quite sure what I'm doing wrong there.
On the Arduino side, I'm using Serial.read() to retrieve the Serial data, which I assume will all be broken down into bytes, so I need to then convert it into a HEX formate, right?
----- Update -----
I now know that I need to break down my color variable into a char-message. I'm guessing I can do something like this then, for example: And then on the other side (Arduino), I need to rebuild the message, right?
if(mousePressed == true){
//Red
myPort.write('2');
myPort.write('5');
myPort.write('5');
myPort.write(',');
//Green
myPort.write('0');
myPort.write('3');
myPort.write('0');
myPort.write(',');
//Blue
myPort.write('0');
myPort.write('0');
myPort.write('0');
myPort.write('\n');
}
My Arduino script is as follows, is this an okay route to go down?
String message;
void loop() {
int _size = Serial.available();
if(_size > 0){
char value = Serial.read();
message += value;
//full message received (255,255,255)
if(message.length() == 11){
String _red = message.substring(0,3);
String _green = message.substring(4,7);
String _blue = message.substring(8,11);
Serial.println("R:" + _red + " G:" + _green + " B:" + _blue);
//apply to light system
setColour(_red.toInt(), _green.toInt(), _blue.toInt());
//clear buffer
message = "";
Serial.flush();
}
}
}
2 Answers 2
Add print(_hexColor);
to you processing sketch. The value it's sending should appear in the Processing Console (at the bottom)
I know I'm late, but actually got a working solution, when I was working out a solution for this.
Code in Processing assuming variable myPort
is handled in setup()
:
void draw() {
int r = 255;
int g = 123;
int b = 2;
myPort.write('S'); // tell the arduino to do something with leds
myPort.write(r);
myPort.write(g);
myPort.write(b);
}
Code for Arduino loop:
void loop() {
int r;
int g;
int b;
if(Serial.available()) {
if(Serial.read() == 'S') {
while(!Serial.available()){}
r = Serial.read();
while(!Serial.available()){}
g = Serial.read();
while(!Serial.available()){}
b = Serial.read();
}
delay(10); // some recovery time, not sure if nessecary
// do something with rgb values
}
}
This works for me with every possible RGB range. It's not the most elegant solution, but by far better then converting between strings/chars and ints with different bit counts on different devices.
-
002 is an octal number. In this case it's still 2, but 020 would be 16. Don't use leading zeros, unless you actually want octal numbers.gre_gor– gre_gor2017年09月04日 18:46:54 +00:00Commented Sep 4, 2017 at 18:46
-
you're right, I wrote this on the fly!Cronay– Cronay2017年09月05日 20:03:28 +00:00Commented Sep 5, 2017 at 20:03
C23,49,119\n
for example). Then it is a case of reading that string and interpreting it properly.