// Adding a callback function example.
//
// In this example we'll setup and demo a callback. As well as read the button state in
// loop(). You don't need the button state stuff in loop() to make the callback stuff
// work. Its just there to show you two things going at once.
#include <mechButton.h>
#include <timeObj.h>
#define BTN_PIN1 3 // Pin we'll hook the button 1 to.
#define BTN_PIN2 2 // Pin we'll hook the button 2 to.
#define RED_PIN 13 // Usual pin number for built in LED.
#define GREEN_PIN 12 // Gren to show state.
#define LONG_MS 1000
mechButton button1(BTN_PIN1); // Set button one to pin BTN_PIN1.
mechButton button2(BTN_PIN2); // Set button one to pin BTN_PIN2.
timeObj btn1Timer(LONG_MS,false);
timeObj btn2Timer(LONG_MS,false);
// Your standard sketch setup()
void setup() {
Serial.begin(115200);
pinMode(RED_PIN,OUTPUT); // Set up the red LED pin for output.
pinMode(GREEN_PIN,OUTPUT); // Set up green LED pin for output.
button1.setCallback(btn1Callback); // Set up our callback.
button2.setCallback(btn2Callback); // Set up our callback.
}
// This is the guy that's called when button1 changes state.
void btn1Callback(void) {
bool state;
state = button1.getState();
digitalWrite(RED_PIN,!state);
if (!state) {
btn1Timer.start();
} else {
if (btn1Timer.ding()) {
Serial.println("normal! (1)");
} else {
Serial.println("up! (1)");
}
btn1Timer.reset();
}
}
// This is the guy that's called when button2 changes state.
void btn2Callback(void) {
bool state;
state = button2.getState();
digitalWrite(GREEN_PIN,!state);
if (!state) {
btn2Timer.start();
} else {
if (btn2Timer.ding()) {
Serial.println("normal! (2)");
} else {
Serial.println("up! (2)");
}
btn2Timer.reset();
}
}
// Your standard sketch loop()
void loop() { idle(); }