// ************************************************************************************************
//
// Documentation for this, and the rest of the LC libraies that make this up.
// Here's the book : https://github.com/leftCoast/LC_libraryDocs/blob/main/LC_libraries.pdf
// Search for : mechButton
//
// ************************************************************************************************
// 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 state;
// Your standard sketch setup()
void setup() {
Serial.begin(115200); // Fire up our serial monitor thing.
state = false;
pinMode(RED_PIN,OUTPUT); // Set up the LED pin for output.
digitalWrite(RED_PIN,state); // 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) {
if (!aButton.getState()) { // If the button has been pressed..
if (state) { // If state..
Serial.println("Do thing 2"); // We'll do 2.
} else { // Else..
Serial.println("Do thing 1"); // we'll do 1.
} //
state = !state; // Swap state.
digitalWrite(RED_PIN,state); // Show state.
}
}
// Your standard sketch loop()
void loop() { idle(); } // Let all the idlers have time to do their thing.