Hi guys,
nothing really fancy coming up here, but I thought I should share my last project with you:
drumrolls… A serially controlled RGB-LED tadaaa (wasn’t that much of a surprise, right?)
It’s really just an RGB-LED hooked up to 3 digital ports and a little Processing sketch to change its color (x = green, y = blue, r = LMB/RMB).
Well, now, with the code cleaned up, it looks really simple, but it really cost me some nerves to get the serial communication going reliably.
In the beginning Arduino and Processing just didn’t want to synchronize. I tried to implement some talk between them, like:
Processing: Hey 'duino, I’ve got some color for you, are you there?
Arduino: Yeah man, send it, im here!
Processing: Here you have it, in your face!
Arduino: Whoa, flashy colors, yum yum, well, i’m gonna sleep a bit, wake me up when you get new colors…
Processing: Ok, see you later alligator!
a bit later…
Processing: Hey dude, i’ve got new colors for you…
and so on…
Long story told short: it didn’t work. Disastrous debugging sessions, but I couldn’t find the mistakes.
So I took an easier approach: Processing just spits out the rgb-value every frame and Arduino waits for 4 bytes to come, an initialization byte and the actual colors.
Thankfully the data sent from processing doesn’t have to be converted from ASCII to DEC (as it had to when controlling the LED via Arduino’s Serial Monitor), so that made the code actually quite small, about 2 kB.
By the way, my intention was to create a simple mood light. Next step to do is adding IR-communication to the LED, so the mood light can be placed apart from the arduino. And eventually every LED-module will get an IR transceiver, so you can put several modules in different places (within line of sight of course) and they synchronize color themselves. Lots of work to come…
But this is the beginning, check it out and leave your comments!
Thanks, Roland
P.S.: Funny, how in hindsight such a piece of hard mental “work” with all its problems and frustrations comes down to such an easy piece of code…I need a time machine…
//Serially cotrolled RGB-LED (by Roland Grichnik)
//17.03.2009
//inspired by the Serial tutorials and some threads on Arduino-Forum
//Thanks to mem and AlphaBeta from the Arduino Forum, which made serial communication another bit (if not a byte...) clearer for me!
byte r = 0;
byte g = 0;
byte b = 0;
byte r_pin = 5;
byte g_pin = 6;
byte b_pin = 3;
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() == 4) {
if (Serial.read() == 'I') {
r = Serial.read();
g = Serial.read();
b = Serial.read();
}
}
analogWrite(r_pin, r);
analogWrite(g_pin, g);
analogWrite(b_pin, b);
}
//Processing Code:
//important: The sketch will only run if you create the AgencyFB-Bold font (size: 100). In processing, click TOOLS-->CREATE FONT to create it in your sketch's data folder.
/*
import processing.serial.*;
Serial myPort;
PFont font;
int r = 0;
int g = 0;
int b = 0;
void setup() {
size(512, 512);
println(Serial.list());
String portName = Serial.list()[1];
myPort = new Serial(this, portName, 9600);
font = loadFont("AgencyFB-Bold-100.vlw");
}
void draw() {
background(0, 0, 0);
smooth();
stroke(50, 50, 50);
fill(r, g, b);
rectMode(CORNERS);
rect(20, 20, width / 2 - 10, height - 20);
textFont(font);
textAlign(CENTER, TOP);
text("COLOR!", width - width / 4 - 5, 20);
textAlign(LEFT);
fill(r, 0, 0);
text("R: ", width - width / 2, 292);
textAlign(RIGHT);
text(r, width - 10, 292);
textAlign(LEFT);
fill(0, g, 0);
text("G: ", width - width / 2, 392);
textAlign(RIGHT);
text(g, width - 10, 392);
textAlign(LEFT);
fill(0, 0, b);
text("B: ", width - width / 2, 492);
textAlign(RIGHT);
text(b, width - 10, 492);
g = mouseX / 2;
b = mouseY / 2;
if ((mousePressed == true) && (mouseButton == LEFT) && (r < 251)) {
r += 5;
}
if ((mousePressed == true) && (mouseButton == RIGHT) && (r > 4)) {
r -= 5;
}
myPort.write('I');
myPort.write(r);
myPort.write(g);
myPort.write(b);
}
*/