Unit 50
How to embed YouTube videos:
RGB Slider Example
import processing.serial.*;
Serial myPort; // declaration
// r01, g01, b01 are values from 0...1
float r01;
float g01;
float b01;
float boxw;
float boxx;
byte myByteArray[];
//=====================================================
void setup() {
size (400, 350);
boxw = width - 100;
boxx = 50;
r01 = 1.0;
g01 = 0.8;
b01 = 0.8;
myPort = new Serial(this, Serial.list()[0], 9600);
myByteArray = new byte[4];
}
//=====================================================
void draw() {
// compute the colors from the RGB values
float rCol = map(r01, 0, 1, 0, 254);
float gCol = map(g01, 0, 1, 0, 254);
float bCol = map(b01, 0, 1, 0, 254);
background(rCol, gCol, bCol);
float rW = map(r01, 0, 1, 0, boxw);
float gW = map(g01, 0, 1, 0, boxw);
float bW = map(b01, 0, 1, 0, boxw);
// draw black rectangles in background
fill (0, 0, 0);
rect (boxx, 50, boxw, 50);
rect (boxx, 150, boxw, 50);
rect (boxx, 250, boxw, 50);
// draw RGB sliders
fill (rCol, 0, 0);
rect (boxx, 50, rW, 50);
fill (0, gCol, 0);
rect (boxx, 150, gW, 50);
fill (0, 0, bCol);
rect (boxx, 250, bW, 50);
// perform interaction which sets the RGB values
if (mousePressed) {
if ((mouseY > 50) && (mouseY < 100)) {
r01 = constrain(map(mouseX-boxx, 0, boxw, 0, 1), 0, 1);
}
else if ((mouseY > 150) && (mouseY < 200)) {
g01 = constrain(map(mouseX-boxx, 0, boxw, 0, 1), 0, 1);
}
else if ((mouseY > 250) && (mouseY < 300)) {
b01 = constrain(map(mouseX-boxx, 0, boxw, 0, 1), 0, 1);
}
int rArd = (int)(rCol); // get integer versions of the floats..
int gArd = (int)(gCol);
int bArd = (int)(bCol);
println(rArd);
myPort.write (rArd);
myPort.write ('R');
myPort.write (gArd);
myPort.write ('G');
myPort.write (bArd);
myPort.write ('B');
}
}
Arduino code:
int led0=3; int led1=5; int led2=6;
int rval=0; int gval=0; int bval=0;
char buff[]= "0000000000";
void setup() {
Serial.begin(9600);
pinMode(led0,OUTPUT);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
}
void loop() {
analogWrite(led0,bval);
analogWrite(led1,gval);
analogWrite(led2,rval);
while (Serial.available()>0) {
for (int i=0; i<10; i++) {
buff[i]=buff[i+1];
}
buff[10]=Serial.read();
if (buff[10]=='R') {
rval=int(buff[9]);
}
if (buff[10]=='G') {
gval=int(buff[9]);
}
if (buff[10]=='B') {
bval=int(buff[9]);
}
}
delay(10);
}
modified from code by Anthony Maddox.