/*
Arduino | coding-help
Multiple functions depending on how long you press the buttons
jim lee — Sunday, November 16, 2025 3:00 PM
https://wokwi.com/projects/447809444941538305
*/
#include <mechButton.h>
mechButton ourBtn(2);
timeObj firstTimer(500, false);
timeObj secondTimer(2000, false);
void setup() {
Serial.begin(115200); // Fire up serial port.
ourBtn.setCallback(btnClk); // Tell the button what callback to ue on change.
}
void btnClk(void) {
if (ourBtn.getState()) { // Button lifted.
if (!firstTimer.ding() && firstTimer.getFraction() != 1) { // If lifted and some time spent before first timer expired..
newPage(); // Call new page.
} //
firstTimer.reset(); // Reset timers.
secondTimer.reset(); // Going back to rest state.
} else { // OH ohh! Button pressed!
firstTimer.start(); // We start the first timer.
} //
}
void newPage(void) {
Serial.println("New Page.");
}
void bumpVariable(void) {
Serial.println("Bump variable.");
}
void loop() {
idle(); // Do the magic. (Runs things like the button.)
if (firstTimer.ding()) { // If first timer has gone off..
secondTimer.start(); // Start second timer.
firstTimer.reset(); // Shut down first timer.
bumpVariable(); // Do initial variable bump.
} //
if (secondTimer.ding()) { // If the second timer goes off..
bumpVariable(); // We bump the variable.
}
// Do whatever you need here. But DON"T CALL delay()!!
// But you can call sleep(). Works the same way but don'y mess up the button stuff.
}