Release date: 2016 Jun 03
This is a library for the Arduino IDE that helps interface with pushbuttons by either reading the state of the button or monitoring it for press/release events.
This library is designed to work with the Arduino IDE versions 1.0.x and 1.5.x, and will probably not work with older versions.
This library supports any Arduino-compatible board.
This library supports many different ways of connecting a button to your board. The simplest way is to select a pin that is not being used for anything else and to connect a normally-open momentary pushbutton from that pin to ground.
Download the pushbutton-arduino library from GitHub, decompress it, and drag the "Pushbutton" folder into the "libraries" subdirectory inside your Arduino sketchbook directory. You can view your sketchbook location by selecting File->Preferences in the Arduino environment. If there is not already a "libraries" folder in that location, you should create it yourself. After installing the library, restart the Arduino environment. Example code for using this library can be found in the File->Examples menu.
To create a Pushbutton object with default settings, which enables the internal pull-up on the pin and interprets a high pin value as the default (unpressed) state of the button, put these lines near the top of your sketch:
Optional arguments can be passed to the constructor to specify other button types and connection methods; see the documentation links below for details.
isPressed() function can be used to directly read the state of the button. This function simply calls
digitalRead to get the current state of the button and does not take care of any debouncing.
The Pushbutton class has several functions that wait for an event on the button. These functions take care of debouncing for you.
waitForPress() function waits until the button is pressed. The
waitForRelease() function waits until the button is released.
Note that if the button is already pressed when
waitForPress() is called, it will return quickly (in 10 ms). If you want to perform an action when the user presses the button but want to avoid performing the action multiple times while the user holds the button down, you will need to call
waitForButton() function waits until the button is pressed, and then waits until the button is released. This function could be used like this:
These functions wait for an event to happen before returning, so your board cannot do much else while it is waiting. They are appropriate for simple programs where a button press is the only event you need to respond to in the main loop.
The Pushbutton class provides two non-blocking functions that allow you to monitor the button for transitions. These functions are powerful and can be used in almost any situation.
getSingleDebouncedPress() function will return true once for each time it detects the button changing from the released state to the pressed state. The
getSingleDebouncedRelease() function will return true once for each time it detects the button changing from the pressed state to the released state.
These functions are non-blocking and are meant to be called repeatedly in a loop: