I've decided to create a library for using buttons on the Arduino platform. See also http://www.arduino.cc/playground/Code/AdvButton.
Features
- Event based implementation
- Recording the time a button is pressed
- Adjustable repeat delay, start delay for the keypressed event
- requires only a single call in the main loop
Download, install and import
Download here: Attach:AdvButton.zipExtract the folder AdvButton under
#include< AdvButton.h >
#include < ButtonManager.h >
See the file buttondemo.pde, included in the zip, for an example.
Usage
After importing the header files, declare the buttons:#define PINBUTTON1 2 #define PINBUTTON2 3 #define PINLIGHT1 8 #define PINLIGHT2 9 AdvButton but1 = AdvButton(PINBUTTON1,OnKeyPressBut1,100,1000); AdvButton but2 = AdvButton(PINBUTTON2,NULL,OnKeyDownBut2,OnKeyUpBut2);
Button 1 will generate a keypress event upon pressing the button at pin 2. After a short delay (1000 milliseconds), every 100 milliseconds the keypress event is raise again. Until the button is released.
Button 2 will generate a keydown event upon pressing the button at pin 3. After releasing the button a keyup event is raised.
Next we need to define the event functions. The triggering button is passed as parameter.
void OnKeyPressBut1(AdvButton* but) { digitalWrite(PINLIGHT1,HIGH); delay(5); digitalWrite(PINLIGHT1,LOW); } void OnKeyDownBut2(AdvButton* but) { digitalWrite(PINLIGHT2,HIGH); } void OnKeyUpBut2(AdvButton* but) { digitalWrite(PINLIGHT2,LOW); }
Last is the adding a call to the buttonmanager to update the button states:
void loop() { ButtonManager::instance()->checkButtons(); }
Hy, I found your job wonderful. I' creating a Auto light for my home.
BeantwoordenVerwijderenI need your help:
Does it is possible to implement a debounce for the input pin (PINBUTTON)?
It would be fantastic if it is possible to have a option in your library so it is possible to decide a time different for every input pin.
I hope that you understand what I mean, sorry for my bad english.
Any Ideas to do that?
THANKS