Buttons

Pull-up Button

This sketch demonstrates the basic implementation of a button or switch, in pull-up configuration, with debouncing functionality for noisy buttons.


The code

#include <CtrlBtn.h>

// Define an onPress handler.
void onPress() {
  Serial.println("Pull-up button pressed");
}

// Define an onRelease handler.
void onRelease() {
  Serial.println("Pull-up button released");
}

/*
  Create a button with:
  - signal pin.
  - bounce duration.
  - onPress handler (optional).
  - onRelease handler (optional).
 */
CtrlBtn button(1, 15, onPress, onRelease);

void setup() {
  Serial.begin(9600);
  // If you use an external pull-up resistor, uncomment this:
  // button.setPinMode(INPUT, PULL_UP);
  // If you use an external pull-down resistor, uncomment this:
  // button.setPinMode(INPUT, PULL_DOWN);
}

void loop() {
  // The process method will poll the button object and handle all it's functionality.
  button.process();
}
Previous
Button methods