// 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.
// Your standard sketch setup()
void setup() {
Serial.begin(115200); // Fire up our serial monitor thing.
pinMode(RED_PIN,OUTPUT); // Set up the LED pin for output.
digitalWrite(RED_PIN,true); // Set true because it ain't pushed yet.
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) {
bool state;
state = aButton.getState();
Serial.print("Button just became "); // We just saw the button change state.
if (state) { // 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.
} //
digitalWrite(RED_PIN,state); // Set the new state.
}
// Your standard sketch loop()
void loop() { idle(); } // Let all the idlers have time to do their thing.