Arduino IDE: RBD_Light Example Blink Without DelayNeed help writing code? You can hire Robots + Big Data to write Arduino software for your project! Click here to get started...
Arduino

ButtonAPI DOCUMENTATION

Read and debounce buttons and switches.

See Example  Public Methods  Release Notes
Overview

Read and debounce buttons and switches without delay or interrupts. React to button events with the onPressed() and onReleased() commands. Control debounce time with setDebounceTimeout().

This library sets each button up as input_pullup by default, allowing you to wire the button to any digital input and ground. Input_pullup can be disabled by passing an optional flag to the constructor() and wiring the button with your own resistor.

Example Wiring
Example SketchArduinoC++
Public Methods

RBD::Button

RBD::Button constructor(pin, [input_pullup])

Create a new button and pass in the Arduino pin number. This library will default to input_pullup and invert the button readings to simplify wiring for you. Pass in the second [optional] parameter as false to disable input_pullup.

EXAMPLE SKETCH
RBD::Button button(2);
// RBD::Button button(3, false); // disable input pullup

void setup() {
  ...
}
button
.setDebounceTimeout(value)

Provide an unsigned long value greater than zero to set the debounce time in milliseconds. Most mechanical buttons will flip between on and off very quickly before settling in the correct state after being pressed or switched. Adjusting the debounce timeout will help remove these extra state-change events.

EXAMPLE SKETCH
void setup() {
  button.setDebounceTimeout(10) // default ms
}
button
.invertReading()

Flip the value of the raw button state reading so when the button isPressed() it will register as isReleased(), and vice versa. This method changes the behavior for onPressed() and onReleased() as well.

EXAMPLE SKETCH
void setup() {
  button.invertReading();
}
button
.isPressed()

Returns true while the button is being pressed, or is on. Returns false while the button is not being pressed, or is off.

This method is good for real-time actions that must fire many times during a button press. Use the onPressed() event to fire only one action on each button press.

EXAMPLE SKETCH
void loop() {
  if(button.isPressed()) {
    // code executes on each loop
    Serial.println("Button Pressed");
  }
}
button
.isReleased()

Returns true while the button is released, or is off. Returns false while the button is being pressed, or is on.

This method is good for real-time actions that must fire many times while the button is not pressed. Use the onReleased() event to fire only one action on each button release.

EXAMPLE SKETCH
void loop() {
  if(button.isReleased()) {
    // code executes on each loop
    Serial.println("Button Released");
  }
}
button
.onPressed()Event

This will return true only once, after the button has been pressed (and debounced). The button must be released and then pressed again for this method to return true again.

EXAMPLE SKETCH
void loop() {
  if(button.onPressed()) {
    // code only runs once per event
    Serial.println("Button Pressed Event");
  }
}
button
.onReleased()Event

This will return true only once, after the button has been released (and debounced). The button must be pressed and then released again for this method to return true again.

EXAMPLE SKETCH
void loop() {
  if(button.onReleased()) {
    // code only runs once per event
    Serial.println("Button Released Event");
  }
}