The following code operates in a way that LED starts to flash only if the pushbutton is pushed for one second. I want to change this and make the LED flash for 5 times, immediately on push, regardless if the button is held "long" or released immediately. Can anyone help me out with this? I'm using the LC_baseTools library btw. I managed to do this with another library with doFlash(5)
but I have to work with LC basetools in this scenario and I can't figure out how to do it.
#include <mechButton.h>
#include <blinker.h>
#define BUTTON_PIN 11
#define LED_PIN 13
#define BLINK_MS 500.0
#define HOLD_MS 1000
mechButton theButton(BUTTON_PIN); // A debounced button object. (Hook button between pin & ground)
blinker theBlinker(LED_PIN,BLINK_MS/2,BLINK_MS); // A fire and forget blinker object.
timeObj pushTimer(HOLD_MS); // A timer object
void setup(void) {
theBlinker.setOnOff(false); // Make sure the blinker is acutally off.
theButton.setCallback(btnClicked); // Set the callback for the button.
}
// This gets called when the button is clicked.
void btnClicked(void) {
if (!theButton.trueFalse()) { // If it has been grounded..
pushTimer.start(); // Start the timer.
theBlinker.setPercent(100); // Set the blinker to full on.
theBlinker.setOnOff(true); // Fire it up.
} else { // Else, button released..
theBlinker.setOnOff(false); // SHut off the blinker.
}
}
void loop(void) {
idle(); // idle runs the stuff in the background (button & binker)
if (!theButton.trueFalse()) { // If the button is currently pressed..
if (pushTimer.ding()) { // If the timer has expired..
theBlinker.setPercent(50); // Start blinking!
}
}
}
pushTimer
to get this long press behavior. Why just not using it by removing the inner if statement inloop()
? have you tried that?raises a flag
in form of a red traffic light ... drivers watch for red light....... put a layer of separation between the button press and the LED blink ... pressing a button sets a flag, nothing else .... the LED blink code watches the flag and blinks when flag is raised .... the flag could have a value 5 ... when the blink code sees the flag being non-zero, then it blinks the LED once and decrements flag value .... on next iteration ofloop()
it does it again ... etc.