/*
RGB LED
This program can set various colours using RGB LED
The RGB LED used is a common cathode type which means the common terminal needs to be grounded.
Connect the negative pin of RGB block to battery ground pin
Connect the red pin of RGB block to Arduino D3 pin
Connect the green pin of RGB block to Arduino D5 pin
Connect the blue pin of RGB block to Arduino D6 pin
*/
int redPin = 11; // declare pin D3 as red pin
int greenPin = 10; //declare pin D5 as green pin
int bluePin = 9; // declare pin D6 as blue pin
void setup() {
//declare the red, green & blue pins as outputs
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
for (int i = 0; i < 256; i++) {
setColor(i, 0, 0); // red
delay(5);
}
}
void loop() {
for (int i = 0; i < 256; i++) {
setColor(255, i, 0); // Orange & yellow
delay(5);
}
for (int i = 255; i >= 0; i--) {
setColor(i, 255, 0); // green
delay(5);
}
for (int i = 255; i >= 0; i--) {
setColor(0, i, (255 - i)); // blue
delay(5);
}
for (int i = 255; i >= 0; i--) {
setColor((255 - i), 0, i); // violet & indigo to red
delay(5);
}
}
// function to set values for each 3 pins of RBG LED
void setColor(int red, int green, int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}