// This example shows mechButton, wich debounces a button then,
// if desired, can do a callback when that button's state changes.
// Along with a timeObj, that works like a kitchen timer. Exceept
// in ms instead of minutes. These two classes can make your
// coding WAY eaiser. And, they are completely NON BLOCKING
//
// Hide the mess of millis!
#include <mechButton.h>
#include <timeObj.h>
#define LED 3 // Pin # for LED.
mechButton redBtn(2); // Set up the button on pin 2
timeObj LEDTimer(5000); // Timer for holding LED on. (ms)
void setup() {
pinMode(LED,OUTPUT); // Make LED pin output.
digitalWrite(LED,LOW); // Make sure the damn thing's off.
redBtn.setCallback(btnClicked); // Hookup the button's callback function.
}
// Button callback.
void btnClicked(void) {
if (!redBtn.trueFalse()) { // If the button was pressed..
digitalWrite(LED,HIGH); // Turn on the LED.
LEDTimer.start(); // Start the timer.
}
}
void loop() {
idle(); // Do magic..
if (LEDTimer.ding()) { // If the timer goes "ding", as timers do..
digitalWrite(LED,LOW); // Shut off the LED.
LEDTimer.reset(); // Turn off the timer.
}
// Do other stuff here..
}