My first project, IMO the "Hello World" of Arduino land was making an LED pulse. I took that right from the built in examples. Having completed that I wanted to go further and do something a little more complicated and write the code from scratch. So I decided to put together a little circuit to drive an RGB LED and cycle it through all (many) of the available colours.
To start I found my kit contained a slightly less popular type of RGB LED. That being a common anode type. This means that the individual Red, Green and Blue cells of the LED share a single positive lead instead of the more common type where the cathode, or negative, lead is shared. No biggie, this just meant that the circuit wiring was slightly different.
So I connected each RGB (negative) lead to a 330ohm resistor and from the resistor to one pin each on the Arduino using pins 9, 10, and 11. I connected the positive (anode) lead to the 5v pin on the Arduino.
Now we're ready to code.
I used to be a really solid programmer back in the day, but I haven't done much since the programming languages all turned convoluted and Object Oriented, but that's another discussion. The Processing language that is used by the Arduino is very much like 'C' and I was pretty comfortable jumping in with the language reference section of the Arduino website up on my computer. I'm going to post my code here and let me say, yes I know there are better/different ways to do this but I chose the method I used because it was easy and because I wanted to experiment with variables and arrays for learning purposes.
// scan through all RGB values with RGB LED on pins 9-11
// Christopher Gaul - Nov 2013
int i = 0;
int j = 0;
int k = 0;
int l = 0;
int m = 0;
int LEDpins [3][3] = {
{9,10,11},
{11,9,10},
{10,11,9}
};
void setup() {
// put your setup code here, to run once:
for (i = 0; i < 3; i++)
{
pinMode(LEDpins[0][i], OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
for (j = 0; j < 3; j++)
{
for (k = 0; k < 254; k += 4)
{
for (l = 0; l < 254; l += 4)
{
for (m = 0; m < 254; m += 4)
{
analogWrite(LEDpins[j][1], 254-k);
analogWrite(LEDpins[j][0], 254-l);
analogWrite(LEDpins[j][2], 254-m);
delay(30);
}
delay(20);
}
delay(10);
}
}
delay(1000);
}
As you can see I chose to use an array to store the RGB pin numbers. This allowed me to use the outside for loop to step through those pin layouts and cycle through every colour combination with each colour taking it's turn as the 'anchor' colour. This happens in the J loop.
The nested K, L, and M loops increment the brightness level of each colour pin. The analogWrite statement sets a duty cycle for each colour pin which effectively sets the brightness for that pins' colour. By varying each brightness level, different colours are created via RGB mixing.
The only odd thing in the loops is that the value sent to the pin is stated as "254-x" where "x" is the loop reference (J,K,L). The reason I did this is that because of the common anode LED, the individual LED elements are active low. I wanted the brightness of each colour to ramp up with each loop instead of ramping down. This accomplished that.
The delays are just arbitrary numbers I found to work by experimenting with each one until a pleasing rate of pulsing and colour changing was achieved. The last delay pauses for one second each time through the main loop.
There's nothing amazing or impressive about this project or my code, I just wanted to share them for my own future reference and to show how easy it is to get up and running with the Arduino.
Reference:
Borderless Electronics - http://borderlesselectronics.org/
BE Maker Kit - http://igg.me/at/bemaker/x/689644
Arduino home - http://arduino.cc/
Arduino Leonardo - http://arduino.cc/en/Main/ArduinoBoardLeonardo
Arduino programming reference - http://arduino.cc/en/Reference/HomePage
As always, I recommend OpenSUSE which can be found at http://www.opensuse.org/en/
Install the Arduino developer and programming software kit on OpenSUSE with one click here - http://playground.arduino.cc/Linux/OpenSUSE


