Tuesday, May 27, 2008

Moodlight 101: fading an RGB LED

By now we all know the the idea of the Moodlight. Philips sort of started the madness (or at least brought it to the masses) with their Livingcolors: a big RGB-LED in a transparent plastic ball. It uses coloured light to set the mood in your room to that of the person holding the remote control.
That's all really great, of course. But it's easy to make something like this by yourself, so, as many have before me, I'll make something of a how-to here. Maybe someone can use it, but at least it'll present thousands with the opportunity to bitch about my silly coding.

The first step (after getting Hello World to work) is connecting an RGB LED, and making a few nice colors using PWM. The idea has been explained about a million times by now, so if you'd like to learn more about it you can read the article at Wikipedia.





Take a random RGB-LED you've got lying around somewhere, and 3 more or less fitting resistors. To make proper colours you'll need to calculate the values you need, based on the specs of the LED, but I didn't know those, and I didn't have any other resistors than these ones (220 ohms, a pretty safe value to make any LED light up at 5V).
This LED has a common anode (+), so in order to make it work you'll need to put a "0" on the pin corresponding to the colour you'd like to see. Because of this, PWMming will be inversed.

As far as code goes, it's pretty simple:

/*
* RGB fading
* One RGB-led (common anode) connected to pins 9, 10 and 11.
* (c) (for what it's worth) 2008, Niels Hordijk
*/

// Define the pins the LED is connected to; be sure to use 3 (of the 6 available) PWM-pins.
int pinR = 9;
int pinG = 11;
int pinB = 10;
int i;

void setup() {
//set pins to output
pinMode(pinR, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinB, OUTPUT);
//turn off the lights
analogWrite(pinR, 255);
analogWrite(pinG, 255);
analogWrite(pinB, 255);
}

void loop() { //main program
for (i=255; i>=0; i--) {
//fade in - red LED
analogWrite(pinR, i);
delay(15);
}
analogWrite(pinR, 255); //and turn it off again
for (i=255; i>=0; i--) {
//fade in - green LED
analogWrite(pinG, i);
delay(15);
}
analogWrite(pinG, 255);
for (i=255; i>=0; i--) {
//fade in - blue LED
analogWrite(pinB, i);
delay(15);
}
analogWrite(pinB, 255);
}


And to post something semi-original: fade from random colour to random colour:

/*
* RGB random fading
* One RGB-led (common anode) connected to pins 9, 10 and 11.
* (c) (for what it's worth) 2008, Niels Hordijk
*/

// Define the pins the LED is connected to; be sure to use 3 (of the 6 available) PWM-pins.
int pinR = 9;
int pinG = 11;
int pinB = 10;
int red = 255;
int green = 255;
int blue = 255;
int redNew;
int greenNew;
int blueNew;
int fadeSteps = 100; //sets fade-resolution
int fadeDelay = 15; //sets fade-delay
int i;

void setup() {
//set pins to output
pinMode(pinR, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinB, OUTPUT);
//turn off the lights
analogWrite(pinR, red);
analogWrite(pinG, green);
analogWrite(pinB, blue);
//randomize the (pseudo) random number generator (check the reference)
randomSeed(analogRead(0));
}

void loop() { //main program
//set random values
redNew = random(256);
greenNew = random(256);
blueNew = random(256);
for (i=1; i<fadeSteps; i++) {
analogWrite(pinR, red+(redNew-red)*i/fadeSteps);
analogWrite(pinG, green+(greenNew-green)*i/fadeSteps);
analogWrite(pinB, blue+(blueNew-blue)*i/fadeSteps);
delay(fadeDelay);
}
red = redNew;
green = greenNew;
blue = blueNew;
}


And a bit of movie to make it slightly less boring:

Thursday, May 15, 2008

The new toy

Okay, it's not really new. The last few months I've been toying around a bit with it, and I think I sort of understand the basic idea now.





This is Boarduino, a clone of open-source dev/prototyping-platform Arduino. It can do everything Arduino can do, and can be plugged directly into a breadboard, which makes it a lot easier to keep track of where the different wires of your little project are actually going to.


Well, sort of. Don't go and plug multiple projects into one board.

In the above picture, the first project is well underway (it's an RGB moodlight), but there still a bunch of little problems to tackle. Here's a video of it:

Labels: , , ,

Tuesday, May 13, 2008

Where was I?

Right. So much for actually posting something about the Olimex board.

After some fiddling around with it, I found out -with a bit of help from the internets- how to get LEDs to blink and how to work a shift register.
There were 2 problems, though: I couldn't get the inputs to work properly (there seemed to be some flicker which caused the thing to register a button-click multiple times, a lot of the time), and it works at 3.3V, which didn't make it very compatible with some other stuff (yeah, both are blue/white, which used to be the proverbial bomb when I ordered them a couple of years ago) I had lying around.

But it had captured my interest, and after finding out about a fun project called Arduino, specifically Ladyada's breadboard-pluggable clone, Boarduino, I decided to switch platforms.

Right now I've got 2 little projects, the first of which is a Moodlight-type device.

So, here I go again, maybe this time the blog will stay alive. The projects will, anyway, and I've actually gotten somewhere so far. And it helps having a friend who's also just started messing with uCs to compete discuss ideas and possibilities with every once in a while ;).

Labels: , , , ,

Wednesday, October 31, 2007

The blog is dead. Long live the blog.

Marvin works quite well, though. But I've rediscovered an old toy, which is shinier (read: it has more I/O ports): an Olimex MSP430F1121 dev-kit.


It has an ultra-low-power TI processor and 14 I/O-ports, which should be enough to connect that old LCD I found in the projects-bin.

So. next project: try to get that MSP430 devboard to work. And then attach the LCD and some rotary encoders to it.

I might actually post/rant some more or less useful info here. Or I'll just forget about this thing again, we'll have to wait and see about that.

Labels: , , ,

Thursday, November 23, 2006

It's alive!

And I've named him Marvin. It's 8k brain is a bit smaller than a planet, though..


After a couple of hours of soldering (and checking resistors - there are a few mistakes in the manual, so you won't get far without a multimeter) it's finished :).
On powering on only one problem surfaced: I had inverted the wires for one of the motors.. but that was easily fixed.

I made a few modifications to the original manual, though: I've used double-sided tape to fix the motors, and some of those twistable cable ties (unlike the provided tie-wraps, these are easy to re-use: a nice advantage when you want to change batteries).

I did send my first Hello World-program trough the IR-transciever, so that works as well.

Tomorrow: real programming :).

Wednesday, November 22, 2006

Asuro?

Asuro is a cute little robot building kit. According to the box, it's a:
  • Complete kit including software and a table tennis ball
which saves the fuss of delving up a table tennis ball somewhere to use as front wheel/slide.
Actually, it's a complete robot, with:
  • 2 small motors to separately drive the back wheels
  • 6 touch sensors ("Hey, this is a wall! Let's turn back around and see what's around there! Oh, another wall!!")
  • 2 light sensors mounted at the bottom, very usefull for following a line (guess what our assignment is...)
  • 2 rotation sensors (read: also light sensors, the rotation bit we'll have to program ourselves)
  • And the coolest bit: IR communication features! I am so going to make this talk with my Palm :).
Programmable in C (or whatever language you can find a compiler for), flashable trough the IR port (how cool is that?), and with half a Christmas tree in status LEDs it'll give away a decent disco show as well.

Linky: the website of the people who designed it.

revive(blog);

So, Animal Crossing wasn't really as fun as I hoped it'd be (and/or I didn't have enough time/energy to actively participate in my new second life as inhabitant of a cute, tiny little seaside village).

New project to blog about: Asuro. More info coming soon.

Sunday, April 16, 2006

Animal Crossing

So, I started a game of Animal Crossing (Wild World, Nintendo DS) today.

The idea is, start a life inside a game. You get dropped into a village (of which you can choose the name), where you get a tiny house from Tom Nook, the local shop owner. Of course, you'll need to pay for that (19.800 bells, the in-game currency), so the first thing I did was help him out with a couple of chores.

After delivering some furniture and greeting everyone in town (just 3 inhabitants, the mayor, the two gatekeepers, the two owls and pigeon (?) at the museum, the post office attendant and the two porcupines at the local fashion store), Tom deducted 1.400 bells from my debt (whoo, that went rather quickly), and set me free for the rest of the day.

I walked around a bit, but couldn't really find something very interesting, so I'll probably try again later.