Materials:
1. Diffuse RGB LED 10mm 1
2. 270 Ω Resistors (red, purple, brown stripes) - you can use up to 1K ohm although it will be a little dimmer
3. Breadboard
4. Arduino board
5. Jumper wires
Tutorial
/*
Adafruit Arduino - Lesson 3. RGB LED
*/
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
//uncomment this line if using a Common Anode LED
//#define COMMON_ANODE
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)
{
#ifdef COMMON_ANODE
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
/*
Adafruit Arduino - Lesson 3. RGB LED
*/
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
//uncomment this line if using a Common Anode LED
//#define COMMON_ANODE
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop()
{
setColor(100, 10, 140); // violet
delay(500);
setColor(100, 10, 200); // violet
delay(500);
setColor(50 ,50, 50); // light blue
delay(500);
setColor(50 ,50, 100); // gb1
delay(500);
setColor(50 ,50, 200); // gb2
delay(500);
setColor(50 ,20, 200); // gb3
delay(500);
setColor(0, 0, 255); // blue
delay(1000);
setColor(255, 0, 0); // red
delay(1000);
setColor(30, 10, 0); // lime green
delay(1000);
setColor(0, 255, 0); // green
delay(1000);
setColor(255, 0, 0); // red
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)
{
#ifdef COMMON_ANODE
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
setColor(0, 0, 255); // blue
The number on the right is the value of the colour blue. The one to the left is red, and the one in the middle is most likely yellow. The reason I said 'most likely' is because the yellow light didn't show up (so there was probably something wrong with my set up).
Anyway, I really like how this LED is brighter and its transition between different colour of light is smoother than the ones used in the previous experiments. I think I enjoy experimenting with colour more than sound (just because the melody I produced sounded squeaky/doesn't sound like music).
Interesting LED projects:
http://www.instructables.com/id/Arduino-Programmable-LED-Infinity-Mirror/
http://www.instructables.com/id/Arduino-Led-Dice/
http://www.instructables.com/id/Arduino-LED-Rocker-Game/
http://www.instructables.com/id/LED-Ring-Clock-With-Arduino/