Code: Select all
/*
Light Box
Rotates red, green, and blue LEDs through a cycle. Looks pretty
inside a light box or jar.
*/
int redPin = 11; // Red LED connected to digital pin 12
int greenPin = 10; // Green LED connected to digital pin 11
int bluePin = 9; // Blue LED connected to digital pin 10
int firstRun = 1;
void setup() // run once, when the sketch starts
{
pinMode(redPin, OUTPUT); // sets the digital pin as output
pinMode(greenPin, OUTPUT); // sets the digital pin as output
pinMode(bluePin, OUTPUT); // sets the digital pin as output
}
// This function fades in the LED
void fadeIn(int led){
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
analogWrite(led, fadeValue);
delay(60); // Change this to make the fade in slower or faster
}
}
// This function fades out the LED
void fadeOut(int led){
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
analogWrite(led, fadeValue);
delay(60); // Change this to make the fade out slower or faster
}
}
void loop(){ // run over and over again
if (firstRun == 1) { // if this is the first iteration, start with red
fadeIn(redPin);
delay(3000);
firstRun = 0;
}
fadeIn(greenPin);
delay(3000); // RED + GREEN
fadeOut(redPin);
delay(3000); // GREEN
fadeIn(bluePin);
delay(3000); // GREEN + BLUE
fadeOut(greenPin);
delay(3000); // BLUE
fadeIn(redPin);
delay(3000); // RED+BLUE
fadeOut(bluePin);
delay(3000); // RED
}