Polling Button Arduino Library

Button is an Arduino library. It can be included in all your programs and will handle software debouncing of the input.

In addition, it can detect rising and falling edges in a functional programming style, or you can attach functions to the rising and falling edges like an interrupt style of code.

 

Installation

Download latest version from GitHub. And unzip the content into a “Button” folder in your Arduino libraries folder. On Windows, you should have a structure similar to this one below once done.

 

How to use it

From within the Arduino IDE, choose Sketch -> Include Library -> Button. Note that you might have to restart your Arduino IDE before the Button library shows in the list.

 

 

Basic example

In order to realize this, just plug a button between pin 12 or your Arduino and a ground pin, like so:

Upload this program and open the Serial Monitor console to see the magic happen!

#include <Button.h>


//each button needs to be declared
Button* simpleButton;

void setup() {

  Serial.begin(9600);
  
  //create a button on pin 12. We invert the logic (invert=true)
  //so that even we are in input pullup mode, a press to the button
  //will be detected as a "on" value and a rising edge.
  simpleButton = new Button(12, INPUT_PULLUP, true);

}

void loop() {

  //this update the status of the button and must be called at every loop
  simpleButton->poll();

  if(simpleButton->rising()){
    //detecting rising edge
    Serial.println("Button went from off to on!");
  }
  else if(simpleButton->falling()){
    //detecting falling edge
    Serial.println("Button went from on to off!");
  }

}

 

 

Interrupt Style functions

This 2nd example does exactly the same thing, but uses function calls once an edge is detected.

#include <Button.h>

//each button needs to be declared
Button* simpleButton;

void onRise(){
  Serial.println("Button went from off to on!");
}

void onFall(){
  Serial.println("Button went from on to off!");
}

void setup() {
  Serial.begin(9600);
  
  //create a button on pin 12. We invert the logic (invert=true)
  //so that even we are in input pullup mode, a press to the button
  //will be detected as a "on" value and a rising edge.
  simpleButton = new Button(12, INPUT_PULLUP, true);

  //attach funtion to be triggered on rise or fall
  simpleButton->attachFunction(&onRise, RISING);
  simpleButton->attachFunction(&onFall, FALLING);
  
}

void loop() {
  //this update the status of the button and must be called at every loop
  simpleButton->poll();

}

 

 

Multiple Buttons

Software debouncing works by using millis(). If you have multiple buttons, it is good practice to do a call only once per loop and supply the value to the buttons; as in:

#include <Button.h>

unsigned long milliseconds = 0UL;

Button* btn1;
Button* btn2;

void setup() {
  Serial.begin(9600);

  //create buttons on pin 12 and 11
  btn1 = new Button(12, INPUT_PULLUP, true);
  btn2 = new Button(11, INPUT_PULLUP, true);

}

void loop() {
  //When using multiple button, it's better to make a global call
  //to millis() and store the value. millis() is needed for the 
  //software debouncer.
  milliseconds = millis();

  btn1->poll(milliseconds);
  btn2->poll(milliseconds);

  if(btn1->getState() == HIGH){
    Serial.println("Button on pin 12 is ON!");
  }
  if(btn2->getState() == HIGH){
    Serial.println("Button on pin 11 is ON!");
  }

  delay(100);

}

 

 

… And that is all!

 

Licensing

This code is licensed under CC-BY 4.0.

 

Leave a comment

Your email address will not be published. Required fields are marked *

css.php