Thursday 31 December 2009

RGB LED

As I said in my previous post, those good people at oomlout sent me a free RGB LED sample so I felt it would be daft to to have a good play with it.
Following the circuit diagram on the piece of paper and using the three resistors taped to the back I built this lovely little bit of gizmometry:










Using the following code I set the the LED to flash red, then green, then blue.


/**
* RGB LED test
*/

int redLed = 11;
int greenLed = 12;
int blueLed = 13;

const boolean ON = LOW;
const boolean OFF = HIGH;

const boolean RED[] = {ON, OFF, OFF};
const boolean GREEN[] = {OFF, ON, OFF};
const boolean BLUE[] = {OFF, OFF, ON};


void setup()
{
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
}

void loop()
{
setColour(RED);
delay (100);
setColour(GREEN);
delay (100);
setColour(BLUE);
delay (100);
}

void setColour(const boolean* colour)
{
digitalWrite (redLed, colour[0]);
digitalWrite (greenLed, colour[1]);
digitalWrite (blueLed, colour[2]);
}


Now this is all well and good, but it isn't very interactive is it? So I decided to add a potentiometer to the circuit and feel the output of the pot to the analogue in of the Arduino, the idea being to control the colour of the LED using the pot.
I also decided that red, green and blue were just too boring, so I also added the secondary colours yellow, cyan and magenta as these can be created by simply mixing red, green and blue.

Then with the following code we read the value of the potentiometer and set the colour accordingly.

/**
* RGB LED test version 2
*/

int redLed = 11;
int greenLed = 12;
int blueLed = 13;
int potPin = 0;
int potValue = 0;

const boolean ON = LOW;
const boolean OFF = HIGH;

const boolean RED[] = {ON, OFF, OFF};
const boolean GREEN[] = {OFF, ON, OFF};
const boolean BLUE[] = {OFF, OFF, ON};
const boolean YELLOW[] = {ON, ON, OFF};
const boolean CYAN[] = {OFF, ON, ON};
const boolean MAGENTA[] = {ON, OFF, ON};
const boolean WHITE[] = {ON, ON, ON};



void setup()
{
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
}

void loop()
{
potValue = analogRead (potPin);
if (potValue > -1 && potValue < 170) {
setColour(RED);
} else if (potValue > 170 && potValue < 341) {
setColour(YELLOW);
} else if (potValue > 341 && potValue < 512) {
setColour(GREEN);
} else if (potValue > 512 && potValue < 682) {
setColour(CYAN);
} else if (potValue > 682 && potValue < 853) {
setColour(BLUE);
} else {
setColour(MAGENTA);
}
}

void setColour(const boolean* colour)
{
digitalWrite (redLed, colour[0]);
digitalWrite (greenLed, colour[1]);
digitalWrite (blueLed, colour[2]);
}

The end result can be seen in this little video:

Getting Started..

Following a recommendation by a friend of mine I decided to order an Arduino Experimentation Kit from oomlout.com

It arrived well packaged in a very nice multi compartment plastic box with everything I need to get going.
Those good people at oomlout also sent me a free sample RGB LED that I shall play with later in this blog

I run an iMac and the software installed very easily (just make sure you have the
latest version) and worked straight off. Just follow the lovely instructions in the Arduino Experimenter's Guide supplied with the kit.

The first thing I did was to unpack the Arduino board and the breadboard and attach them to the acrylic base plate provided. I then tried the simple 'Blinking LED' example provided with the kit.

The software is good if a little basic, but it is very easy to use. My advice tho is to keep the experimenter's guide to hand and use it to check your syntax by looking at their examples.

All in all this little thing looks awesome. Iona, I owe you a drink.