├── README.md ├── library.properties ├── library.json ├── eBtn.h ├── src ├── eBtn.h └── eBtn.cpp ├── eBtn.cpp └── examples └── InterruptButton └── InterruptButton.ino /README.md: -------------------------------------------------------------------------------- 1 | # eBtn 2 | An Arduino library to create buttons and handle events using callbacks. 3 | The library handle the following events: 4 | 5 | - press 6 | - release 7 | - hold 8 | - longPress 9 | 10 | **Usage** 11 | 12 | eBtn btn = eBtn(btnPin); 13 | 14 | btn.on("press",callbackFn); 15 | 16 | void callbackFn(){ 17 | //do something here 18 | } 19 | 20 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=eBtn 2 | version=1.0 3 | author=Davide Andreazzini 4 | maintainer=Davide Andreazzini 5 | sentence=A library to handle Buttons based on events. 6 | paragraph=Supports the following events : [press , hold, release, long] 7 | category=Signal Input/Output 8 | url=https://github.com/david1983/eBtn 9 | architectures=avr,esp8266 -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "eBtn", 3 | "keywords": "button, events, long press, release, hold, click", 4 | "description": "The eBtn library allow to create a button specifying a pin and add the following events to the btn [press , hold, release, long]", 5 | "repository": 6 | { 7 | "type": "git", 8 | "url": "https://github.com/david1983/eBtn" 9 | }, 10 | "frameworks": "arduino", 11 | "platforms": "atmelavr" 12 | } 13 | -------------------------------------------------------------------------------- /eBtn.h: -------------------------------------------------------------------------------- 1 | #ifndef eBtn_h 2 | #define eBtn_h 3 | 4 | typedef void (*callBack) (); 5 | 6 | class eBtn{ 7 | public: 8 | eBtn(int pin); 9 | void handle(); 10 | void setThreshold(int t); 11 | void on(String eventName, callBack cb); 12 | private: 13 | int _pin,_status,_prevStatus; 14 | int _pressThrsld,_pressTime,_startPress; 15 | bool _readed; 16 | String _event; 17 | callBack _press, _release, _hold, _long; 18 | }; 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /src/eBtn.h: -------------------------------------------------------------------------------- 1 | #ifndef eBtn_h 2 | #define eBtn_h 3 | 4 | typedef void (*callBack) (); 5 | 6 | class eBtn{ 7 | public: 8 | eBtn(int pin); 9 | void handle(); 10 | void setThreshold(int t); 11 | void on(String eventName, callBack cb); 12 | private: 13 | int _pin,_status,_prevStatus; 14 | int _pressThrsld,_pressTime,_startPress; 15 | bool _readed; 16 | String _event; 17 | callBack _press, _release, _hold, _long; 18 | }; 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /eBtn.cpp: -------------------------------------------------------------------------------- 1 | #include "Arduino.h" 2 | #include "eBtn.h" 3 | 4 | typedef void (*callBack) (); 5 | 6 | // Initialize the button on the specified pin 7 | eBtn::eBtn(int pin){ 8 | _pin=pin; 9 | pinMode(_pin, OUTPUT); 10 | digitalWrite(_pin,HIGH); 11 | _status = digitalRead(pin); 12 | _pressThrsld = 1000; 13 | delay(100); 14 | }; 15 | 16 | //check the status of the button, this function must be called inside a loop() or inside an interrupt 17 | void eBtn::handle(){ 18 | _prevStatus = _status; 19 | _status = digitalRead(_pin); 20 | if(_prevStatus==_status && _status==1)_event=""; 21 | 22 | else if(_prevStatus!=_status && _status==0){ 23 | _event="press"; 24 | _startPress = millis(); 25 | } 26 | else if(_prevStatus==_status && _status==0)_event="hold"; 27 | else if(_prevStatus!=_status && _status==1 ){ 28 | if(millis()-_startPress>_pressThrsld){ 29 | _event="long"; 30 | }else{ 31 | _event="release"; 32 | } 33 | _startPress=0; 34 | } 35 | 36 | if(_event=="press")_press(); 37 | if(_event=="hold")_hold(); 38 | if(_event=="release")_release(); 39 | if(_event=="long")_long(); 40 | } 41 | 42 | void eBtn::on(String eventName, callBack cb){ 43 | if(eventName=="press")_press = cb; 44 | if(eventName=="hold")_hold = cb; 45 | if(eventName=="release")_release = cb; 46 | if(eventName=="long")_long = cb; 47 | 48 | } 49 | 50 | void eBtn::setThreshold(int t){ 51 | _pressThrsld = t; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /src/eBtn.cpp: -------------------------------------------------------------------------------- 1 | #include "Arduino.h" 2 | #include "eBtn.h" 3 | 4 | typedef void (*callBack) (); 5 | 6 | // Initialize the button on the specified pin 7 | eBtn::eBtn(int pin){ 8 | _pin=pin; 9 | pinMode(_pin, OUTPUT); 10 | digitalWrite(_pin,HIGH); 11 | _status = digitalRead(pin); 12 | _pressThrsld = 1000; 13 | delay(100); 14 | }; 15 | 16 | //check the status of the button, this function must be called inside a loop() or inside an interrupt 17 | void eBtn::handle(){ 18 | _prevStatus = _status; 19 | _status = digitalRead(_pin); 20 | if(_prevStatus==_status && _status==1)_event=""; 21 | 22 | else if(_prevStatus!=_status && _status==0){ 23 | _event="press"; 24 | _startPress = millis(); 25 | } 26 | else if(_prevStatus==_status && _status==0)_event="hold"; 27 | else if(_prevStatus!=_status && _status==1 ){ 28 | if(millis()-_startPress>_pressThrsld){ 29 | _event="long"; 30 | }else{ 31 | _event="release"; 32 | } 33 | _startPress=0; 34 | } 35 | 36 | if(_event=="press")_press(); 37 | if(_event=="hold")_hold(); 38 | if(_event=="release")_release(); 39 | if(_event=="long")_long(); 40 | } 41 | 42 | void eBtn::on(String eventName, callBack cb){ 43 | if(eventName=="press")_press = cb; 44 | if(eventName=="hold")_hold = cb; 45 | if(eventName=="release")_release = cb; 46 | if(eventName=="long")_long = cb; 47 | 48 | } 49 | 50 | void eBtn::setThreshold(int t){ 51 | _pressThrsld = t; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /examples/InterruptButton/InterruptButton.ino: -------------------------------------------------------------------------------- 1 | /* 2 | InterruptButton 3 | 4 | Simple sketch to control a button on pin 0 using 5 | the eBtn library and Interrupt 6 | 7 | The circuit: 8 | pushButton attached on pin 0 9 | 10 | Created 11 May 2016 11 | By Davide Andreazzini 12 | 13 | https://github.com/david1983/eBtn 14 | 15 | */ 16 | #include 17 | 18 | float n; 19 | // Initialization of an eBtn on pin 0; 20 | const int btnPin = 0; 21 | eBtn btn = eBtn(btnPin); 22 | 23 | void setup() { 24 | Serial.begin(115200); 25 | // Here events are defined 26 | btn.on("press",pressFunc); 27 | btn.on("hold",holdFunc); 28 | btn.on("release",releaseFunc); 29 | btn.on("long",longPressFunc); 30 | //setting the interrupt on btn pin to react on change state 31 | attachInterrupt(btnPin, pin_ISR, CHANGE); 32 | } 33 | 34 | //function to handle the interrupt event 35 | void pin_ISR(){ 36 | btn.handle(); 37 | } 38 | 39 | 40 | //callbacks functions 41 | void pressFunc(){ 42 | n = millis(); 43 | Serial.println("Btn pressed"); 44 | } 45 | 46 | void releaseFunc(){ 47 | Serial.println("Btn released after " + String((millis()-n) /1000) + " seconds"); 48 | } 49 | 50 | void longPressFunc(){ 51 | Serial.println("Btn released after a long press of " + String((millis()-n) /1000) + " seconds"); 52 | } 53 | 54 | void holdFunc(){ 55 | Serial.println("Btn hold for: " + String((millis()-n) /1000) + " seconds"); 56 | } 57 | 58 | 59 | 60 | 61 | void loop() { 62 | //btn::handle() is the function that constantly check for the btn states. 63 | 64 | for(int i=0;i<100;i++){ 65 | delay(500); 66 | Serial.println("This is inside a for loop"); 67 | } 68 | 69 | } --------------------------------------------------------------------------------