├── Button.cpp ├── Button.h ├── LICENSE ├── README.md ├── examples └── basic_usage │ └── basic_usage.ino ├── keywords.txt └── library.properties /Button.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Button - a small library for Arduino to handle button debouncing 3 | 4 | MIT licensed. 5 | */ 6 | 7 | #include "Button.h" 8 | #include 9 | 10 | Button::Button(uint8_t pin, uint16_t debounce_ms) 11 | : _pin(pin) 12 | , _delay(debounce_ms) 13 | , _state(HIGH) 14 | , _ignore_until(0) 15 | , _has_changed(false) 16 | { 17 | } 18 | 19 | void Button::begin() 20 | { 21 | pinMode(_pin, INPUT_PULLUP); 22 | } 23 | 24 | // 25 | // public methods 26 | // 27 | 28 | bool Button::read() 29 | { 30 | // ignore pin changes until after this delay time 31 | if (_ignore_until > millis()) 32 | { 33 | // ignore any changes during this period 34 | } 35 | 36 | // pin has changed 37 | else if (digitalRead(_pin) != _state) 38 | { 39 | _ignore_until = millis() + _delay; 40 | _state = !_state; 41 | _has_changed = true; 42 | } 43 | 44 | return _state; 45 | } 46 | 47 | // has the button been toggled from on -> off, or vice versa 48 | bool Button::toggled() 49 | { 50 | read(); 51 | return has_changed(); 52 | } 53 | 54 | // mostly internal, tells you if a button has changed after calling the read() function 55 | bool Button::has_changed() 56 | { 57 | if (_has_changed) 58 | { 59 | _has_changed = false; 60 | return true; 61 | } 62 | return false; 63 | } 64 | 65 | // has the button gone from off -> on 66 | bool Button::pressed() 67 | { 68 | return (read() == PRESSED && has_changed()); 69 | } 70 | 71 | // has the button gone from on -> off 72 | bool Button::released() 73 | { 74 | return (read() == RELEASED && has_changed()); 75 | } 76 | -------------------------------------------------------------------------------- /Button.h: -------------------------------------------------------------------------------- 1 | /* 2 | Button - a small library for Arduino to handle button debouncing 3 | 4 | MIT licensed. 5 | */ 6 | 7 | #ifndef Button_h 8 | #define Button_h 9 | #include "Arduino.h" 10 | 11 | class Button 12 | { 13 | public: 14 | Button(uint8_t pin, uint16_t debounce_ms = 100); 15 | void begin(); 16 | bool read(); 17 | bool toggled(); 18 | bool pressed(); 19 | bool released(); 20 | bool has_changed(); 21 | 22 | const static bool PRESSED = LOW; 23 | const static bool RELEASED = HIGH; 24 | 25 | private: 26 | uint8_t _pin; 27 | uint16_t _delay; 28 | bool _state; 29 | uint32_t _ignore_until; 30 | bool _has_changed; 31 | }; 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Michael D K Adams 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Button 2 | ====== 3 | 4 | * Author: Michael Adams () 5 | * Copyright (C) 2016 Michael D K Adams. 6 | * Released under the MIT license. 7 | 8 | Button is a tiny library to make reading buttons very simple. It handles debouncing automatically, and monitoring of state. 9 | 10 | Motivation 11 | ---------- 12 | Ahh buttons. Ahh debouncing! Sometimes you really wish you could ignore all the mechanics of debouncing, reading inputs, monitoring state, and just say... `if (button.pressed())` and know that it'll only run once each time you press a button. 13 | 14 | Or how about `if (button.released())`, or `button.toggled()`. It is such a simple concept, but in practice you need to set up timers, monitor input puts, set pullups, etc etc. On anything more than the most simple example, that can become quite a headache. 15 | 16 | So fed up with all that I figured there had to be a better way. This library is the result. 17 | 18 | Features 19 | -------- 20 | * Super simple API. 21 | * Handles debouncing. 22 | * Sets the pin mode automatically. 23 | * Lets you write code that triggers: 24 | ** based on the pin state (high or low) 25 | ** when a button is pressed 26 | ** when a button is released 27 | ** or when a button changes (i.e. pressing or releasing) 28 | 29 | Requirements 30 | ------------ 31 | * An Arduino — http://arduino.cc/ 32 | * A button 33 | 34 | Installation 35 | ------------ 36 | Download the ZIP archive (https://github.com/madleech/Button/zipball/master), then open the Arduino IDE and choose Sketch > Include Library > Add .ZIP Library... and select your downloaded file. 37 | 38 | You should now see in File > Examples > Button entires for the basic\_usage example. 39 | 40 | Code Examples 41 | ------------- 42 | Here is the 'basic\_usage' example program, included in the download: 43 | 44 | ```C++ 45 | #include 46 | 47 | Button button1(2); // Connect your button between pin 2 and GND 48 | Button button2(3); // Connect your button between pin 3 and GND 49 | Button button3(4); // Connect your button between pin 4 and GND 50 | 51 | void setup() { 52 | button1.begin(); 53 | button2.begin(); 54 | button3.begin(); 55 | 56 | while (!Serial) { }; // for Leos 57 | Serial.begin(9600); 58 | } 59 | 60 | void loop() { 61 | if (button1.pressed()) 62 | Serial.println("Button 1 pressed"); 63 | 64 | if (button2.released()) 65 | Serial.println("Button 2 released"); 66 | 67 | if (button3.toggled()) { 68 | if (button3.read() == Button::PRESSED) 69 | Serial.println("Button 3 has been pressed"); 70 | else 71 | Serial.println("Button 3 has been released"); 72 | } 73 | } 74 | ``` 75 | 76 | Documentation 77 | ------------- 78 | **Button(int pin)** 79 | Creates a new Button. 80 | 81 | **void begin()** 82 | Call this in your `setup` method to setup the button. All it does is set the correct pin mode. 83 | 84 | **bool pressed()** 85 | Returns true when _and only when_ the button is pressed. Until the button is released (in the debounced-sense of the word) this function won't return true again. So in effect, it returns true only while you are pressing the button, or to put it another way, it fires on a rising edge. 86 | 87 | **bool released()** 88 | Like `pressed()`, but round the other way. So if you hold down a button, and then release it... that is when it fires. 89 | 90 | **bool toggled()** 91 | Returns true whenever the button is pressed or released, i.e., its position is toggled. To find out what the position actually is, you can use the `read()` function. 92 | 93 | **bool read()** 94 | Returns the current debounced state of the button, i.e. Button::PRESSED or Button::RELEASED. 95 | 96 | **bool has_changed()** 97 | Returns whether the position/state of the button has changed after calling the previous read() function. Unlikely to be used except by Super Gurus. 98 | 99 | Quirks and Things to Keep in Mind 100 | --------------------------------- 101 | **Highs and lows, lows and highs** 102 | The easiest way to connect a switch on an Arduino is to connect it between an input pin and ground, and use the internal pullup resistor to make sure it doesn't float. This is fine and dandy, but it can get a bit confusing, as a "pressed" button is logic level: low, while a "released" button is logic level: high. 103 | 104 | So to make it a bit more obvious what you're talking about, you can use a couple of handy shortcuts: `Button::PRESSED` and `Button::RELEASED` which map to the expected values. 105 | 106 | License 107 | ------- 108 | Copyright (c) 2016 Michael D K Adams. http://www.michael.net.nz/ 109 | 110 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 111 | 112 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 113 | 114 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 115 | 116 | -------------------------------------------------------------------------------- /examples/basic_usage/basic_usage.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | Button button1(2); // Connect your button between pin 2 and GND 4 | Button button2(3); // Connect your button between pin 3 and GND 5 | Button button3(4); // Connect your button between pin 4 and GND 6 | 7 | void setup() { 8 | button1.begin(); 9 | button2.begin(); 10 | button3.begin(); 11 | 12 | while (!Serial) { }; // for Leos 13 | Serial.begin(9600); 14 | } 15 | 16 | void loop() { 17 | if (button1.pressed()) 18 | Serial.println("Button 1 pressed"); 19 | 20 | if (button2.released()) 21 | Serial.println("Button 2 released"); 22 | 23 | if (button3.toggled()) { 24 | if (button3.read() == Button::PRESSED) 25 | Serial.println("Button 3 has been pressed"); 26 | else 27 | Serial.println("Button 3 has been released"); 28 | } 29 | } -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map for Debounce library 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | Button KEYWORD1 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | 15 | read KEYWORD2 16 | toggled KEYWORD2 17 | pressed KEYWORD2 18 | released KEYWORD2 19 | has_changed KEYWORD2 20 | 21 | ####################################### 22 | # Constants (LITERAL1) 23 | ####################################### 24 | PRESSED LITERAL1 25 | RELEASED LITERAL1 26 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Button 2 | version=1.0.0 3 | author=Michael Adams 4 | maintainer=Michael Adams 5 | sentence=Button is a tiny library to make reading buttons very simple. 6 | paragraph=It handles debouncing automatically, and monitoring of state. 7 | category=Signal Input/Output 8 | url=http://utrainia.com/ 9 | architectures=* --------------------------------------------------------------------------------