LEDs

LED set

This sketch demonstrates how to drive an LED directly from a boolean state, using the set() method. Here the LED mirrors a button: it lights up while the button is held down and goes out when it is released.


The code

#include <CtrlLed.h>
#include <CtrlBtn.h>

// The pin of the board's built-in LED. Some boards (e.g. the ESP32 Dev Module)
// don't define LED_BUILTIN, so pin 2 is used there. Change it to match your wiring.
#ifdef LED_BUILTIN
const uint8_t LED_PIN = LED_BUILTIN;
#else
const uint8_t LED_PIN = 2;
#endif

// An LED on the built-in LED pin (digital mode, on/off only).
CtrlLed led(LED_PIN);

// A button on pin 4, with 15ms debouncing, using the internal pull-up resistor.
// (Not pin 2: that is where the LED goes on boards without LED_BUILTIN.)
CtrlBtn button(4, 15);

void setup()
{
  // Nothing to set up.
}

void loop() {
  // Poll the button.
  button.process();

  // Let the LED follow the button's state.
  led.set(button.isPressed());
}
Previous
Fading LED