int led1Pin = 9; // LED 1 connected to PWM pin 9
int led2Pin = 10; // LED 2 connected to PWM pin 10
int brightness = 0; // Initial brightness
int fadeAmount = 5; // Amount to fade the LEDs by
void setup() {
pinMode(led1Pin, OUTPUT); // Set pin 9 as an output
pinMode(led2Pin, OUTPUT); // Set pin 10 as an output
}
void loop() {
// Set the brightness of LED 1 to be increasing:
analogWrite(led1Pin, brightness);
// Set the brightness of LED 2 to be decreasing:
analogWrite(led2Pin, 255 - brightness);
// Change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// Reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// Wait for 10 milliseconds to see the dimming effect
delay(10);
}