Advertisement
rossy__

Arduino Button Library

Feb 27th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. //Button.h
  2.  
  3. /*
  4.     Button - a small library for Arduino to handle button debouncing
  5.    
  6.     MIT licensed.
  7. */
  8.  
  9. #ifndef Button_h
  10. #define Button_h
  11. #include "Arduino.h"
  12.  
  13. class Button
  14. {
  15.     public:
  16.         Button(uint8_t pin, bool pressed);
  17.         void begin();
  18.         bool read();
  19.         bool toggled();
  20.         bool pressed();
  21.         bool released();
  22.         bool has_changed();
  23.        
  24.         const static bool PRESSED = LOW;
  25.         const static bool RELEASED = HIGH;
  26.    
  27.     private:
  28.         uint8_t  _pin;
  29.         uint16_t _delay;
  30.         bool     _state;
  31.         bool     _has_changed;
  32.         uint32_t _ignore_until;
  33. };
  34.  
  35. #endif
  36.  
  37. ////////////////////////////////////////////////////////////////////////////////
  38.  
  39. //Button.cpp
  40.  
  41. /*
  42.     Button - a small library for Arduino to handle button debouncing
  43.    
  44.     MIT licensed.
  45. */
  46.  
  47. #include "Button.h"
  48. #include <Arduino.h>
  49.  
  50. Button::Button(uint8_t pin, bool pressed)
  51. :  _pin(pin)
  52. ,  _delay(100)
  53. ,  _state(HIGH)
  54. ,  _has_changed(false)
  55. ,  _ignore_until(0)
  56. {
  57. }
  58.  
  59. void Button::begin()
  60. {
  61.     pinMode(_pin, INPUT_PULLUP);
  62. }
  63.  
  64. //
  65. // public methods
  66. //
  67.  
  68. bool Button::read()
  69. {
  70.     // ignore pin changes until after this delay time
  71.     if (_ignore_until > millis())
  72.     {
  73.         // ignore any changes during this period
  74.     }
  75.    
  76.     // pin has changed
  77.     else if (digitalRead(_pin) != _state)
  78.     {
  79.         _ignore_until = millis() + _delay;
  80.         _state = !_state;
  81.         _has_changed = true;
  82.     }
  83.    
  84.     return _state;
  85. }
  86.  
  87. // has the button been toggled from on -> off, or vice versa
  88. bool Button::toggled()
  89. {
  90.     read();
  91.     return has_changed();
  92. }
  93.  
  94. // mostly internal, tells you if a button has changed after calling the read() function
  95. bool Button::has_changed()
  96. {
  97.     if (_has_changed == true)
  98.     {
  99.         _has_changed = false;
  100.         return true;
  101.     }
  102.     return false;
  103. }
  104.  
  105. // has the button gone from off -> on
  106. bool Button::pressed()
  107. {
  108.     if (read() == PRESSED && has_changed() == true)
  109.         return true;
  110.     else
  111.         return false;
  112. }
  113.  
  114. // has the button gone from on -> off
  115. bool Button::released()
  116. {
  117.     if (read() == RELEASED && has_changed() == true)
  118.         return true;
  119.     else
  120.         return false;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement