LEDs
Fading LED
This sketch demonstrates the basic implementation of how to set up and control a fading LED
The code
NOTE: Fading requires a PWM-capable pin. Create the LED with a maxBrightness parameter to enable PWM mode.
#include <CtrlLed.h>
// PWM mode example (PWM-capable pin with brightness control):
// This assumes you have connected your LED to a pin with PWM capabilities:
// The maximum brightness (second parameter) can be used to calibrate an LED,
// in case you are using multiple LEDs that have different maximum brightness.
CtrlLed led(LED_BUILTIN, 50);
void setup()
{
// Turn on the LED and set the brightness to 0%.
led.turnOn();
led.setBrightness(0);
}
void loop() {
// Fade LED brightness from 0 to 100.
for (uint8_t brightness = 0; brightness < 100; brightness++) {
led.setBrightness(brightness);
delay(10); // Adjust the delay time to change the transition speed.
}
// Fade LED brightness from 100 to 0.
for (uint8_t brightness = 100; brightness > 0; brightness--) {
led.setBrightness(brightness);
delay(10); // Adjust the delay time to change the transition speed.
}
}