// ************************************************************************************************
//
// 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, timeObj.h
//
// ************************************************************************************************
#include <timeObj.h>
#include <mechButton.h>
#define GRN_BTN 4
#define RED_BTN 3
#define GRN_LED 6
#define RED_LED 7
#define START_HOLD_MS 500
mechButton grnBtn(GRN_BTN); // Auto debounced button.
mechButton redBtn(RED_BTN); // This guy too..
timeObj btnTimer(START_HOLD_MS);
int value;
float holdMs;
void setup() {
Serial.begin(115200); // Fire up serial.
pinMode(GRN_LED, OUTPUT); // Make LED output.
pinMode(RED_LED, OUTPUT); // This one too.
grnBtn.setCallback(greenClick); // What to call when this button changes state.
redBtn.setCallback(redClick); // What to call when this other button changes state.
value = 6; // Some random start value for the counter.
Serial.print("Inital value : "); // Tell Mrs user whats up.
Serial.println(value); //
holdMs = START_HOLD_MS; // Timer hold inital value.
}
// Up or down, we bump the counter here.
void bumpUpDn(bool upDn) {
if (upDn) { // If we want up..
value++; // Bump up the counter.
} else { // Else..
value--; // Bump dn the counter.
} //
Serial.print("Value : "); // Tell Mrs user about it.
Serial.println((value)); //
}
// When the GREEN button changes state, it'll call this function.
void greenClick(void) {
if (!grnBtn.getState()) { // If it's been grounded..
digitalWrite(GRN_LED,HIGH); // Turn on green LED.
bumpUpDn(true); // Twiddle the value up.
holdMs = START_HOLD_MS; // Reset hold time to slow.
btnTimer.setTime(holdMs); // Sets time and starts the timer.
} else { // Else its a lift!
digitalWrite(GRN_LED,LOW); // Turnt off green LED.
btnTimer.reset(); // Turn the timer off.
}
}
// When the RED button changes state, it'll call this function.
void redClick(void) {
if (!redBtn.getState()) { // If it's been grounded..
digitalWrite(RED_LED,HIGH); // Turnt on red LED.
bumpUpDn(false); // Twiddle the value down.
holdMs = START_HOLD_MS; // Reset hold time to slow.
btnTimer.setTime(holdMs); // Sets time and starts the timer.
} else { // Else its a lift!
digitalWrite(RED_LED,LOW); // Turnt off red LED.
btnTimer.reset(); // TUrn the timer off.
}
}
void loop() {
idle(); // idle() runs the all the magic.
if (!grnBtn.getState() && btnTimer.ding()) { // If green is down and the timer has expired..
bumpUpDn(true); // Bump up the count.
holdMs = holdMs/2; // Make the wait, twice as fast.
btnTimer.setTime(holdMs); // Set timer twice as fast, and restart it.
} //
if (!redBtn.getState() && btnTimer.ding()) { // If red is down and the timer has expired..
bumpUpDn(false); // Bump down the count.
holdMs = holdMs/2; // Make the wait, twice as fast.
btnTimer.setTime(holdMs); // Set timer twice as fast, and restart it.
}
}