// Adding a callback function example.
//
// In this example we'll setup and demo a callback. As well as read the button state in
// loop().
#include <mechButton.h>
#include <timeObj.h>
#define BTN_PIN 3 // Pin we'll hook the button to.
#define RED_PIN 13 // Usual pin number for built in LED.
#define GREEN_PIN 12 // Gren to show state.
#define LATCH_MS 1000 // Latch time.
mechButton button(BTN_PIN); // Set button one to pin BTN_PIN2.
timeObj latchTimer(LATCH_MS,false);
// Your standard sketch setup()
void setup() {
pinMode(RED_PIN,OUTPUT); // Set up the red LED pin for output.
pinMode(GREEN_PIN,OUTPUT); // Set up green LED pin for output.
button.setCallback(btnCallback); // Set up our callback.
}
// This is the guy that's called when button1 changes state.
void btnCallback(void) {
if (!button.getState()) {
digitalWrite(GREEN_PIN,HIGH);
digitalWrite(RED_PIN,HIGH);
latchTimer.start();
} else {
digitalWrite(GREEN_PIN,LOW);
}
}
// Your standard sketch loop()
void loop() {
idle();
if (latchTimer.ding()) {
digitalWrite(RED_PIN,LOW);
latchTimer.reset();
}
}