{NOT BLYNK RELATED} Physical long press button state

hey guys, so i have a non-latching push button attached to a digital pin of an esp8266, i want to implement dual functionality of that physical button. a short press and a long press.
i have implemented short press but i need help implementing long press.
so this is the part of the code for short press.

    void SwitchState0(){
  boolean state = (digitalRead(Switch0));
  if ((state) == LOW  && SwitchReset0 == true){
   if ((DeviceState0) == 0){
   }SwitchReset0 = false;
}else if (state){
    // reset flag the physical button release
    SwitchReset0 = true;

for that you have to use blynk timers

  // start a timer to check for a long press
  if (param.asInt()) {
    ButtonTimer = timer.setTimeout(750, LongHoldDetect);

As it’s a physical switch, I think I’d attach an interrupt to the pin that is triggered when the button is pressed.
Once the interrupt triggers then grab the current milis value as your start time.
Then start a timer that samples the value of the pin say every 100 milliseconds.
Once routine that samples the pin detects that the button is released then grab the millis value for the end time.
Stop the timer then deduct the start time from the end time to see how many milliseconds the button was pressed for.
An if statement can do the rest - greater than or equal to your long press time runs one process, else run the regular process.


1 Like

@Sai_Khurana FYI, just adding a disclaimer on the title doesn’t make it proper to post… else this forum will fill up with non-Blynk questions that can be answered with a Google search :stuck_out_tongue: