// Adding a callback function example.
//
// In this example we'll setup a callback for a mechButton.
#include <mechButton.h>
#define BTN_PIN 2 // Pin we'll hook the button to. The other side hooks to ground.
#define RED_PIN 13 // Usual pin number for built in LED.
mechButton aButton(BTN_PIN); // Set button one to pin BTN_PIN.
bool LEDOn;
// Your standard sketch setup()
void setup() {
Serial.begin(9600); // Fire up our serial monitor thing.
pinMode(RED_PIN,OUTPUT); // Set up the LED pin for output.
LEDOn = false; // LED is OFF.
aButton.setCallback(myCallback); // Set up our callback. (Also calls hookup() for idling.)
}
// This is the guy that's called when the button changes state.
void myCallback(void) {
Serial.print("Button just became "); // We just saw the button change state.
if (aButton.getState()) { // If the button state is now high.
Serial.println("true!"); // Tell the user.
} else { // Else it just bacame low. (Grounded, pushed)
Serial.println("false!"); // Tell user.
LEDOn = !LEDOn; // Swap the LED state.
digitalWrite(GREEN_PIN,state); // Set the new state. (Make it true)
}
}
// Your standard sketch loop()
void loop() {
idle(); // Let all the idlers have time to do their thing.
}