Triggering led flash after x seconds of active pushbutton

I am trying to do the following, as a total copy paste beginner - have the LED turn on with a pushbutton - but in case the pushbutton is held for x amount of seconds (2 secs for example) - the led would start to blink until I release the pushbutton.

I managed to find a library that detects the pushbutton state and starts the flash when pushbutton is pushed - but it stars right after the button is activated.

How would I tailor this for my requirement;

  • push button pressed for a length less than 1 second - LED steady on
  • push button pressed for a length longer than 1 second - LED blinks on-off

Thanks!

#include <Bounce2.h>


#define BUTTON_PIN 11
#define LED_PIN 13

// Instantiate a Bounce object
Bounce debouncer = Bounce(); 

int buttonState;
unsigned long buttonPressTimeStamp;

int ledState;

void setup() {
  
  Serial.begin(57600);
  
  // Setup the button
  pinMode(BUTTON_PIN,INPUT);
  // Activate internal pull-up
  digitalWrite(BUTTON_PIN,HIGH);
  
  // After setting up the button, setup debouncer
  debouncer.attach(BUTTON_PIN);
  debouncer.interval(5);
  
  //Setup the LED
  pinMode(LED_PIN,OUTPUT);
  digitalWrite(LED_PIN,ledState);
  
}

void loop() {
 // Update the debouncer and get the changed state
  boolean changed = debouncer.update();


  
  if ( changed ) {
       // Get the update value
     int value = debouncer.read();
    if ( value == HIGH) {
       ledState = LOW;
       digitalWrite(LED_PIN, ledState );
   
       buttonState = 0;
       Serial.println("Button released (state 0)");
   
   } else {
          ledState = HIGH;
       digitalWrite(LED_PIN, ledState );
       
         buttonState = 1;
         Serial.println("Button pressed (state 1)");
         buttonPressTimeStamp = millis();
     
   }
  }
  
  if  ( buttonState == 1 ) {
    if ( millis() - buttonPressTimeStamp >= 500 ) {
         buttonPressTimeStamp = millis();
         if ( ledState == HIGH ) ledState = LOW;
         else if ( ledState == LOW ) ledState = HIGH;
         digitalWrite(LED_PIN, ledState );
        Serial.println("Retriggering button");
    }
  }
 
 
}
1 Like

The next thing on your to-do list is IDE → file examples/digital/blinkwithoutdelay (Blink without delay() explained line-by-line). Follow that with @Robin2’s Several things at the same time tutorial.

Kudos for code tags on your first post!

It’s a bit overwhelming, i’m just a lil ole hobbyist doing 10 things at once :smiley: is there a way to alter the “if ( buttonState == 1 )” part of the code posted above and tweak it so it “waits” for a second before it starts to blink?

Well, you could try it this way…

#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 acutaly 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!
      }
   }
}

You’ll need to install LC_baseTools from the library manager to compile this.

Good luck & have fun!

-jim lee

1 Like

Magic! It works! Thanks so much. I’ll try to adapt the rest of my code with your library.

One question though! What would be the workaround for solving the off-pattern button release? What I mean is this; if you keep the button pushed long enough to start the flash, the flashing starts, but if you release the button at that point, you might release it right as the LED starts to flash, which results in a flash that looks “glitchy” for a lack of better word.

How could I make the last flash play until the end AFTER I release the button?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.