Arduino Sketch Created by Simon Monk

The following test sketch will cycle through the colors red, green, blue, yellow, purple, and aqua. These colors being some of the standard Internet colors.
Copy Code
/*
Adafruit Arduino - Lesson 3. RGB LED
*/

int redPin = 11;
int greenPin = 10;
int bluePin = 9;

void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);  
}

void loop()
{
  setColor(255, 0, 0);  // red
  delay(1000);
  setColor(0, 255, 0);  // green
  delay(1000);
  setColor(0, 0, 255);  // blue
  delay(1000);
  setColor(255, 255, 0);  // yellow
  delay(1000);  
  setColor(80, 0, 80);  // purple
  delay(1000);
  setColor(0, 255, 255);  // aqua
  delay(1000);
}

void setColor(int red, int green, int blue)
{
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);  
}
Try the sketch out and then we will dissect it in some detail......

The sketch starts by specifying which pins are going to be used for each of the colors:
Copy Code
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
The next step is to write the 'setup' function. As we have learnt in earlier lessons, the setup function runs just once after the Arduino has reset. In this case, all it has to do is define the three pins we are using as being outputs.
Copy Code
void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);  
}
Before we take a look at the 'loop' function, lets look at the last function in the sketch.
Copy Code
void setColor(int red, int green, int blue)
{
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);  
}
This function takes three arguments, one for the brightness of the red, green and blue LEDs. In each case the number will be in the range 0 to 255. Where 0 means off and 255 means maximum brightness. The function then calls 'analogWrite' to set the brightness of each LED.
If you look at the 'loop' function you can see that we are setting the amount of red, green and blue light that we want to display and the pausing for a second before moving on to the next color.
Copy Code
void loop()
{
  setColor(255, 0, 0);  // red
  delay(1000);
  setColor(0, 255, 0);  // green
  delay(1000);
  setColor(0, 0, 255);  // blue
  delay(1000);
  setColor(255, 255, 0);// yellow
  delay(1000);  
  setColor(80, 0, 80);  // purple
  delay(1000);
  setColor(0, 255, 255);// aqua
  delay(1000);
}
Try adding a few colors of your own to the sketch and watch the effect on your LED.
If you are using a Common Anode RGB LED, then you will need to change the analog write values so that the color is subtracted from 255, like this: analogWrite(redPin, 255-red); for each of the three colors.
Last updated on 2013-01-21 at 09.23.52 AM