
Fading LED Brightness Code
int led = 9; //Defines, or sets the name led to be used in pin 9
int brightness = 0; //Variable that sets the brightness of the LED
int fadeAmount = 5; //Variable that sets the rate the light will change, or brighten/dim
void setup() {
pinMode(led, OUTPUT); //Sets the the pin that is defined by led as an output
}
void loop() {
analogWrite(led, brightness); //Writes (sends) the varying brightness value assigned to the LED
brightness = brightness + fadeAmount; //Takes the value of brightness and adds the fade amount
//The following if statement reverses the brightness when the variable reaches a maximum, 255 or a minimum, 0
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount; //Changes brightness value to its opposite integer value
}
delay(30); //Sets the wait time (in milliseconds) before running the loop
}