Let's Make Robots!

RGB Mixer – Processing to Arduino

send values from processing to arduino

It has been a long time since I wanted to control the arduino with processing and I tried a lot of libraries, and a lot of processes and I always felt that any of those were suited for what I needed. I needed something simple to implement and easy to understand, and because I am not a programmer, I asked for help and @pauloricca replied to me with a quick, fast and really good solution.

In this example I have connected an RGB LED to the Arduino and on Processing we will have a simple mixer to fade in and out color channels. DON’T DO THIS, LED’s always need to have one resistor in series before. In this case I just wanted to show the serial communication part, and I skipped the resistor part, lazy me! Never do this, otherwise you will kill your leds very fast.

RGB Mixer - Processing to Arduino

On the Arduino side, I defined 3 digital output pins 9, 10, 11, these are PWM capable pins. Than I defined pin 8 as an OUTPUT, and digitallyWrite it to LOW, to be the GROUND pin. On the loop() function we used a switch() function that detects when the sync characters ’R’, ‘G’ and ‘B’ are received. These characters will tell us what value is coming next. The function GetFromSerial() is called everytime we need to read a value from the serial port.

void setup()
{
// declare the serial comm at 9600 baud rate
Serial.begin(9600);

// output pins
pinMode(9, OUTPUT); // red
pinMode(10, OUTPUT); // green
pinMode(11, OUTPUT); // blue

// another output pin o be used as GROUND
pinMode(8, OUTPUT); // ground
digitalWrite(8, LOW);
}

void loop()
{
// call the returned value from GetFromSerial() function
switch(GetFromSerial())
{
case 'R':
analogWrite(9, GetFromSerial());
break;
case 'G':
analogWrite(11, GetFromSerial());
break;
case 'B':
analogWrite(10, GetFromSerial());
break;

}
}

// read the serial port
int GetFromSerial()
{
while (Serial.available()<=0) {
}
return Serial.read();
}

On the Processing side, I am using a slider class adapted from anthonymatox.com, and I created 3 instances of this class (I assume you understand the concept of class). The important thing to understand here is the import of the Serial library, and the creation of a Serial object called “port”. On the setup() function I print out the available serial ports and than I defined which one is the Arduino port, on my case is the number 0, note that I am using mac, if you are using PC it should be COM1, COM2 or another COM#. Finally I am passing the values of the slider after I pass the sync character ‘R’, ‘G’, ‘B’.

RGB Mixer - Processing to Arduino

import processing.serial.*;
Serial port;

sliderV sV1, sV2, sV3;

color cor;

void setup() {
size(500, 500);

println("Available serial ports:");
println(Serial.list());

// check on the output monitor wich port is available on your machine
port = new Serial(this, Serial.list()[0], 9600);

// create 3 instances of the sliderV class
sV1 = new sliderV(100, 100, 90, 255, #FF0000);
sV2 = new sliderV(200, 100, 90, 255, #03FF00);
sV3 = new sliderV(300, 100, 90, 255, #009BFF);
}

void draw() {
background(0);

sV1.render();
sV2.render();
sV3.render();

// send sync character
// send the desired value
port.write('R');
port.write(sV1.p);
port.write('G');
port.write(sV2.p);
port.write('B');
port.write(sV3.p);
}

/*
Slider Class - www.guilhermemartins.net
based on www.anthonymattox.com slider class
*/
class sliderV {
int x, y, w, h, p;
color cor;
boolean slide;

sliderV (int _x, int _y, int _w, int _h, color _cor) {
x = _x;
y = _y;
w = _w;
h = _h;
p = 90;
cor = _cor;
slide = true;
}

void render() {
fill(cor);
rect(x-1, y-4, w, h+10);
noStroke();
fill(0);
rect(x, h-p+y-5, w-2, 13);
fill(255);
text(p, x+2, h-p+y+6);

if (slide==true && mousePressed==true && mouseX<x+w && mouseX>x){
if ((mouseY<=y+h+150) && (mouseY>=y-150)) {
p = h-(mouseY-y);
if (p<0) {
p=0;
}
else if (p>h) {
p=h;
}
}
}
}
}

RGB Mixer - Processing to Arduino

RGB Mixer - Processing to Arduino

RGB Mixer - Processing to Arduino

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

hrmmm...2 things.

In the arduino code you have a switch/case but don't have a default case. From what the manual says, it's not necessary but should be included for good coding practices.

The getfromserial method uses <=, less than or equal to.  value.available() >0 would seem to make more sense

If nothing comes through this value will be -1(per manual) again, no default case to catch this.

Just some recommended changes.

Aside from that, it's a pretty cool project.  :)

I agree, but if we go for good practices I have start by using the resistors in series before the led  :) ....  regarding the code I don't understand your 2nd suggestion.. If you feel it is really important please send it to me and I will change the code, thnx  ;)

I should have said some changes that might make things more robust if you get into more complex coding.

I hear ya though, good circuit design is a different story and I take all suggestions when I can get em.  :)

As for the changes, I only mention them because things can tend to bite you in the butt later if you don't take into account all possible values. As for the <= thing, I guess you are guaranteed to do something even if you don't have a valid value for the switch select(you had a, b, and c, but -1 would be a value you'd also get on an empty read) as in the case of -1 being returned for the .available() method. I assume the switch/case without the "default" case just ignores the value and goes back to the loop. My suggestion is to close off that unknown by making sure that you only catch the values you are looking for by only reading if there is a value hence the .available() > 0. What you have works though, I just like playing around with the code sometimes to make sure I know that it's doing what I expect it to...  :)

I changed the 'A, 'B', 'C' sync characters to 'R', 'G' and 'B'

Calculon has never used processing until now, and this was a perfect way to get into it for him.
He's been building on the sketch to add Save/Load functions.

 

(save/load isn't working yet). The LED is part of a dekstop polysmurf flower:

Note: the RGB that Calculon used (the RadioShack variety) uses a common anode. Thus, he had to change the 'duino sketch to pull LOW for each PWM pin, and map the variables to switch high and low (since 255 = off).

Thanks for your trailblazing, Guibot!