# Long press button option

**URL:** <https://forum.arduino.cc/t/long-press-button-option/1020430>\
**Category:** Programming\
**Created:** [August 8, 2022, 6:47pm UTC](https://forum.arduino.cc/t/long-press-button-option/1020430 "2022-08-08T18:47:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leetut](https://avatars.discourse-cdn.com/v4/letter/l/f05b48/32.png) [@Leetut](https://forum.arduino.cc/u/Leetut)\
**Post date:** [August 8, 2022, 6:47pm UTC](https://forum.arduino.cc/t/long-press-button-option/1020430/1 "2022-08-08T18:47:54Z")

</div>

Hi, is there an easy way to add a long press option to my button?  
without using a button library

```cpp
  unsigned long buttonPressTime;

  byte upButtonState = digitalRead (upButton);
  if (upButtonState != oldUpButtonState) {
    if (millis () - buttonPressTime >= debounceTime) { // debounce
      buttonPressTime = millis (); // when we closed the switch
      oldUpButtonState = upButtonState; // remember for next time

      if (upButtonState == LOW && mainMenu == true) { // if button up pressed & main menu is active

        menuPage ++; //increment the current menu page's position count
        if (menuPage > 4) {
          menuPage = 0; //0-4 (5 menu pages)
        }
      }
   }
}

```

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [August 8, 2022, 6:55pm UTC](https://forum.arduino.cc/t/long-press-button-option/1020430/2 "2022-08-08T18:55:16Z")

</div>

1. When the switch is detected as **closed** , record the time from millis().

2. When the switch is detected as **opened** , print **millis() minus the time in 1**.

3. In #2, if this time \>= the time for a long press, do your stuff . . .

---

<div class="post-metadata">

**Author:** ![blh64](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blh64/32/605986_2.png) [@blh64](https://forum.arduino.cc/u/blh64)\
**Post date:** [August 8, 2022, 7:26pm UTC](https://forum.arduino.cc/t/long-press-button-option/1020430/3 "2022-08-08T19:26:58Z")

</div>

WIth that approach, whatever should happen will a long press will only happen after the button is released which could be substantially longer than what is defined as a long press. The user would never know if they have pressed the button long enough or not. It is better to to act immediately once you have exceeded your long press time so the user knows it has happened.

To do this, outside your debounce routine, check the state of the button and the duration and then act. You will also need to note that a long press action has happened so you don't continuously retrigger.

```cpp
unsigned long buttonPressTime;
bool longPressTriggered;
const unsigned long longPressTime = 5000;

byte upButtonState = digitalRead (upButton);
if (upButtonState != oldUpButtonState) {
  if (millis () - buttonPressTime >= debounceTime) { // debounce
    buttonPressTime = millis (); // when we closed the switch
    oldUpButtonState = upButtonState; // remember for next time
    longPressTriggered = false;

    if (upButtonState == LOW && mainMenu == true) { // if button up pressed & main menu is active

      menuPage ++; //increment the current menu page's position count
      if (menuPage > 4) {
        menuPage = 0; //0-4 (5 menu pages)
      }
    }
  }
}

if ( longPressTriggered == false && upButtonState == LOW && millis() - buttonPressTime >= longPressTime ) {
  // do your long press event
  longPressTriggered = true;
}

```

---

<div class="post-metadata">

**Author:** ![pmagowan](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@pmagowan](https://forum.arduino.cc/u/pmagowan)\
**Post date:** [August 8, 2022, 8:05pm UTC](https://forum.arduino.cc/t/long-press-button-option/1020430/4 "2022-08-08T20:05:19Z")

</div>

Just understand the concepts. Detecting when a button becomes pressed, debouncing and detecting when it becomes unpressed again. Then it is just a matter of comparison in standard millis timing. It doesn’t matter what the “event” is that triggers your timing it is always the same. Short press, long press, double press etc

---

<div class="post-metadata">

**Author:** ![macgman2000](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/macgman2000/32/1098565_2.png) [@macgman2000](https://forum.arduino.cc/u/macgman2000)\
**Post date:** [August 8, 2022, 9:06pm UTC](https://forum.arduino.cc/t/long-press-button-option/1020430/5 "2022-08-08T21:06:29Z")

</div>

I worked on this debounce and button press capture function. It needs to be modified a bit for a long press, but the frame work is there.

```cpp
void readButtonPress(){
  
  static byte areUnpressed = PINB & B00011111; // define a protected byte areUnpressed with mask B00011111 to focus on pins of interest to portD upper byte PINB status register (pin 15 to pin 8). 
 
       
    areUnpressed = PINB & B00011111; // capture masked pins status register
    if (areUnpressed != B00011111){ // detect whether or not the pin status has changed. meaning buttons are pressed
       myTime3 = (millis() - myTime2); // subtract current system timer from previously captured timer to get how many milliseconds passed since first pass
       if (myTime3 > 10){ // has 10 milliseconds passed? if so capture current millisecond timer value, move status of pressed buttons to loadTX_Decode ready for transmit, zero out myTime3 used in 10 millisecond counter to set up next 10 millisecond debounce.
         myTime2= millis();  
         loadTX_Decode = areUnpressed;  
         myTime3 =0;             
       }         
    }else{ // if buttons not pressed zero out myTime2 and myTime3, reload pin status register to areUnpressed, move pin status to loadTX_Decode ready to transmit
      myTime2 = 0;
      myTime3 = 0;
      areUnpressed = PINB & B00011111; // since buttons were unpressed expected value B00011111
      loadTX_Decode = areUnpressed ; // load above mentioned into transmit byte 
    }
}             
  

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 4, 2023, 9:06pm UTC](https://forum.arduino.cc/t/long-press-button-option/1020430/6 "2023-02-04T21:06:51Z")

</div>

Closed on February 4, 2023, 9:06pm UTC

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