AdvButton library for the Arduino platform by: - Bart Meijer (brupje@gmail.com) If you like this library, or have any other comments, please drop me a line.
(2010/05/06) 1.2: Fixed the library to be compatible with Arduino 1.0.3
(2010/05/06) 1.1: New release
(2010/24/01) 1.0: Initial release
Download here: Attach:AdvButton_12.zip
Extract the folder AdvButton under <Arduino home>/libraries/. (Re)start the Arduino application and select from the menu bar: "Sketch->Import Library->AdvButton". The following lines should be added:
#include <AdvButton.h> #include <ButtonManager.h>
See the file buttondemo.pde, included in the zip, for an example.
Version 1.1: Attach:AdvButton_11.zip
Version 1.0: Attach:AdvButton.zip
After importing the header files, declare the buttons:
#define PINBUTTON1 2 #define PINBUTTON2 0 #define PINLIGHT1 8 #define PINLIGHT2 9 AdvButton but1 = AdvButton(PINBUTTON1,OnKeyPressBut1,100,1000,btn_Digital); AdvButton but2 = AdvButton(PINBUTTON2,NULL,OnKeyDownBut2,OnKeyUpBut2,btn_analog);
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 (on analog pin 0) 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();
}