1 /*! \file Pushbutton.h
2 *
3 * See the Pushbutton class reference for more information about this library.
4 *
5 * \class Pushbutton Pushbutton.h
6 * \brief Read button presses and releases with debouncing
7 *
8 */
10 #ifndef Pushbutton_h
11 #define Pushbutton_h
13 #include <Arduino.h>
15 #define ZUMO_BUTTON 12
17 #define PULL_UP_DISABLED 0
18 #define PULL_UP_ENABLED 1
20 #define DEFAULT_STATE_LOW 0
21 #define DEFAULT_STATE_HIGH 1
23 class Pushbutton
24 {
25 public:
27 // constructor; takes arguments specifying whether to enable internal pull-up
28 // and the default state of the pin that the button is connected to
29 Pushbutton(unsigned char pin, unsigned char pullUp = PULL_UP_ENABLED, unsigned char defaultState = DEFAULT_STATE_HIGH);
31 // wait for button to be pressed, released, or pressed and released
32 void waitForPress();
33 void waitForRelease();
34 void waitForButton();
36 // indicates whether button is currently pressed
37 boolean isPressed();
39 // more complex functions that return true once for each button transition
40 // from released to pressed or pressed to released
41 boolean getSingleDebouncedPress();
42 boolean getSingleDebouncedRelease();
44 private:
46 unsigned char _pin;
47 unsigned char _pullUp;
48 unsigned char _defaultState;
49 unsigned char gsdpState;
50 unsigned char gsdrState;
51 unsigned int gsdpPrevTimeMillis;
52 unsigned int gsdrPrevTimeMillis;
53 boolean initialized;
55 inline void init()
56 {
57 if (!initialized)
58 {
59 initialized = true;
60 init2();
61 }
62 }
64 // initializes I/O pin for use as button input
65 void init2();
67 boolean _isPressed();
68 };
70 #endif