int ledPin = 6; // PWM pin connected to the LED
int brightness = 51; // Start at 20% brightness (20% of 255 = 51)
int stepSize = 26; // 10% of 255 (round-off to closest integer)
void setup() {
pinMode(ledPin, OUTPUT); // Set pin as output
}
void loop() {
// Glow up from 20% to 100% in steps of 10% with 20ms delay
for (int i = brightness; i <= 255; i += stepSize) {
analogWrite(ledPin, i);
delay(20);
}
// Glow down from 100% to 20% in steps of 10% with 20ms delay
for (int i = 255; i >= brightness; i -= stepSize) {
analogWrite(ledPin, i);
delay(20);
}
// Glow up again with 20ms delay per step
for (int i = brightness; i <= 255; i += stepSize) {
analogWrite(ledPin, i);
delay(20);
}
// Glow down again with 80ms delay per step
for (int i = 255; i >= brightness; i -= stepSize) {
analogWrite(ledPin, i);
delay(120);
}
}