Lara Grant

RGB Color Fade

INPUT: Button
OUTPUT: RGB LED

DESCRIPTION: Push button, LED fades through colors.

[code lang=”arduino” light=”true”]

// when button is pushed, led fades through colors
//
// LilyPad RGB LED is common anode so inverse values for common cathode LEDs
//

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

int fadeSpeed = 15; // make this higher to slow down

void setup() {
Serial.begin(9600);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode (buttonPin, INPUT_PULLUP);

}

void loop() {
int buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
int r, g, b;

if (buttonState == LOW) {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin,LOW);

// fade from violet to red
for (b = 0; b < 255; b++) {
analogWrite(bluePin, b);
delay(fadeSpeed);
}
// fade from red to yellow
for (g = 255; g > 0; g–) {
analogWrite(greenPin, g);
delay(fadeSpeed);
}
// fade from yellow to green
for (r = 0; r < 255; r++) {
analogWrite(redPin, r);
delay(fadeSpeed);
}
// fade from green to teal
for (b = 255; b > 0; b–) {
analogWrite(bluePin, b);
delay(fadeSpeed);
}
// fade from teal to blue
for (g = 0; g < 255; g++) {
analogWrite(greenPin, g);
delay(fadeSpeed);
}
}
else{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin,HIGH);

}

}
[/code]

Have your say