int ledPin = 9; // LED connected to digital pin 9
int brightness = 0; // How bright the LED is
int fadeAmount = 5; // How many points to fade the LED by
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
}
void loop() {
analogWrite(ledPin, brightness); // Set the brightness of the LED
brightness = brightness + fadeAmount; // Change the brightness for next time through the loop
// Reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount; // Change the direction of the fade
}
delay(50); // Wait for 30 milliseconds to see the dimming effect
}