Sunday, January 16, 2011

Project 3 - Fade 2 LEDs


/*
Project 3: Fade 2 LEDs

Alternates Fades between 2 LEDs

http://arduinosketches.blogspot.com/
*/

int i = 0; // Integer used for counting

void setup() {
pinMode(11, OUTPUT); // Sets LED Pins
pinMode(9, OUTPUT);
pinMode(5, OUTPUT);
pinMode(3, OUTPUT);
}

void loop(){

for (i = 0; i < 255; i++) { // loops from 0 to 254 (fade in)

analogWrite(9, i); // set Red LED brightness
analogWrite(3, -i); // set Green LED brightness
delay(10); //Without the delay, the fades are not visible
}
for (i = 255; i > 0; i--) { //loop from 255 to 1 (fade out)
analogWrite(9, i); //set Red LED brightness
analogWrite(3, -i); //Green the LED brightness
delay(10);
}
}



This script alters the intensity of light emitted from 2 LEDs. By using the 'analogWrite' function, we tell the Arduino which LED to fade, and at what interval based on the current count of our integer 'i.'

For an extra challenge, try adding a third LED and fade the LEDs from left to right, and back again.

No comments:

Post a Comment