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.4: Added support for rotary encoders. Some code clean-up
(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_14.zip Δ
Check also my Github for releases: https://github.com/brupje/AdvButton
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.2: Attach:AdvButton_12.zip
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(); }
You can hook up a rotary encoder like a normal button. You only need to define the encoder pins, an valueChanged event en optionally a max value.
void OnKeyPressBut1(AdvButton* but) {
// Encoder button pressed
}
void OnEncoderValueCHanged(AdvEncoder* but) {
// read the current rotary value int val = but->getValue();
}
// 10 is the max value. If this is set to -1, it will not have a max value. AdvEncoder but1 = AdvEncoder(ENCA,ENCB,OnEncoderValueCHanged,10,PINBUTTON1,OnKeyPressBut1);