Arduino - Button - Long Press Short Press

We will learn:

In the first three parts, we learn how to detect in principle.

In the last part, we learn how to detect in practical use by applying the debounce. See why do we need to debounce for button. Without debouncing, we may detect wrong the button short press.

About Button

If you do not know about button (pinout, how it works, how to program ...), learn about them in the following tutorials:

Wiring Diagram

Arduino Button Wiring Diagram

This image is created using Fritzing. Click to enlarge image

In this tutorial, we will use the internal pull-up resistor. Therefore, the state of the button is HIGH when normal and LOW when pressed.

How To Detect Short Press

We measure the time duration between the pressed and released events. If the duration is shorter than a defined time, the short press event is detected.

Let's see step by step:

  • Define how long the maximum of short press lasts
const int SHORT_PRESS_TIME = 500; // 500 milliseconds
  • Detect the button is pressed and save the pressed time
if(lastState == HIGH && currentState == LOW) pressedTime = millis();
  • Detect the button is released and save the released time
if(lastState == LOW && currentState == HIGH) releasedTime = millis();
  • Calculate press duration and
long pressDuration = releasedTime - pressedTime;
  • Determine the short press by comparing the press duration with the defined short press time.
if( pressDuration < SHORT_PRESS_TIME ) Serial.println("A short press is detected");

Arduino Code for detecting the short press

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-long-press-short-press */ // constants won't change. They're used here to set pin numbers: const int BUTTON_PIN = 7; // the number of the pushbutton pin const int SHORT_PRESS_TIME = 500; // 500 milliseconds // Variables will change: int lastState = LOW; // the previous state from the input pin int currentState; // the current reading from the input pin unsigned long pressedTime = 0; unsigned long releasedTime = 0; void setup() { Serial.begin(9600); pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: currentState = digitalRead(BUTTON_PIN); if(lastState == HIGH && currentState == LOW) // button is pressed pressedTime = millis(); else if(lastState == LOW && currentState == HIGH) { // button is released releasedTime = millis(); long pressDuration = releasedTime - pressedTime; if( pressDuration < SHORT_PRESS_TIME ) Serial.println("A short press is detected"); } // save the the last state lastState = currentState; }

Quick Steps

  • Upload the above code to Arduino via Arduino IDE
  • Press the button shortly several times.
  • See the result on Serial Monitor
COM6
Send
A short press is detected
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

The Serial Monitor may show several short press detection for one press. This is the normal behavior of the button. This behavior is called the “chattering phenomenon”. The issue will be solved in the last part of this tutorial.

How To Detect Long Press

There are two use cases for detecting the long press.

  • The long-press event is detected right after the button is released
  • The long-press event is detected during the time the button is being pressed, even the button is not released yet.

In the first use case, We measure the time duration between the pressed and released events. If the duration is longer than a defined time, the long-press event is detected.

In the second use case, After the button is pressed, We continuously measure the pressing time and check the long-press event until the button is released. During the time button is being pressed. If the duration is longer than a defined time, the long-press event is detected.

Arduino Code for detecting long press when released

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-long-press-short-press */ // constants won't change. They're used here to set pin numbers: const int BUTTON_PIN = 7; // the number of the pushbutton pin const int LONG_PRESS_TIME = 1000; // 1000 milliseconds // Variables will change: int lastState = LOW; // the previous state from the input pin int currentState; // the current reading from the input pin unsigned long pressedTime = 0; unsigned long releasedTime = 0; void setup() { Serial.begin(9600); pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: currentState = digitalRead(BUTTON_PIN); if(lastState == HIGH && currentState == LOW) // button is pressed pressedTime = millis(); else if(lastState == LOW && currentState == HIGH) { // button is released releasedTime = millis(); long pressDuration = releasedTime - pressedTime; if( pressDuration > LONG_PRESS_TIME ) Serial.println("A long press is detected"); } // save the the last state lastState = currentState; }

Quick Steps

  • Upload the above code to Arduino via Arduino IDE
  • Press and release the button after one second.
  • See the result on Serial Monitor
COM6
Send
A long press is detected
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

The long-press event is only detected right after the button is released

Arduino Code for detecting long press during pressing

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-long-press-short-press */ // constants won't change. They're used here to set pin numbers: const int BUTTON_PIN = 7; // the number of the pushbutton pin const int LONG_PRESS_TIME = 1000; // 1000 milliseconds // Variables will change: int lastState = LOW; // the previous state from the input pin int currentState; // the current reading from the input pin unsigned long pressedTime = 0; bool isPressing = false; bool isLongDetected = false; void setup() { Serial.begin(9600); pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: currentState = digitalRead(BUTTON_PIN); if(lastState == HIGH && currentState == LOW) { // button is pressed pressedTime = millis(); isPressing = true; isLongDetected = false; } else if(lastState == LOW && currentState == HIGH) { // button is released isPressing = false; } if(isPressing == true && isLongDetected == false) { long pressDuration = millis() - pressedTime; if( pressDuration > LONG_PRESS_TIME ) { Serial.println("A long press is detected"); isLongDetected = true; } } // save the the last state lastState = currentState; }

Quick Steps

  • Upload the above code to Arduino via Arduino IDE
  • Press and release the button after several seconds.
  • See the result on Serial Monitor
COM6
Send
A long press is detected
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

The long-press event is only detected when the button is not released yet

How To Detect Both Long Press and Short Press

Short Press and Long Press after released

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-long-press-short-press */ // constants won't change. They're used here to set pin numbers: const int BUTTON_PIN = 7; // the number of the pushbutton pin const int SHORT_PRESS_TIME = 1000; // 1000 milliseconds const int LONG_PRESS_TIME = 1000; // 1000 milliseconds // Variables will change: int lastState = LOW; // the previous state from the input pin int currentState; // the current reading from the input pin unsigned long pressedTime = 0; unsigned long releasedTime = 0; void setup() { Serial.begin(9600); pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: currentState = digitalRead(BUTTON_PIN); if(lastState == HIGH && currentState == LOW) // button is pressed pressedTime = millis(); else if(lastState == LOW && currentState == HIGH) { // button is released releasedTime = millis(); long pressDuration = releasedTime - pressedTime; if( pressDuration < SHORT_PRESS_TIME ) Serial.println("A short press is detected"); if( pressDuration > LONG_PRESS_TIME ) Serial.println("A long press is detected"); } // save the the last state lastState = currentState; }

Quick Steps

  • Upload the above code to Arduino via Arduino IDE
  • Long and short press the button.
  • See the result on Serial Monitor

※ NOTE THAT:

The Serial Monitor may show several short press detection when long press. This is the normal behavior of the button. This behavior is called the “chattering phenomenon”. The issue will be solved in the last part of this tutorial.

Short Press and Long Press During pressing

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-long-press-short-press */ // constants won't change. They're used here to set pin numbers: const int BUTTON_PIN = 7; // the number of the pushbutton pin const int SHORT_PRESS_TIME = 1000; // 1000 milliseconds const int LONG_PRESS_TIME = 1000; // 1000 milliseconds // Variables will change: int lastState = LOW; // the previous state from the input pin int currentState; // the current reading from the input pin unsigned long pressedTime = 0; unsigned long releasedTime = 0; bool isPressing = false; bool isLongDetected = false; void setup() { Serial.begin(9600); pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: currentState = digitalRead(BUTTON_PIN); if(lastState == HIGH && currentState == LOW) { // button is pressed pressedTime = millis(); isPressing = true; isLongDetected = false; } else if(lastState == LOW && currentState == HIGH) { // button is released isPressing = false; releasedTime = millis(); long pressDuration = releasedTime - pressedTime; if( pressDuration < SHORT_PRESS_TIME ) Serial.println("A short press is detected"); } if(isPressing == true && isLongDetected == false) { long pressDuration = millis() - pressedTime; if( pressDuration > LONG_PRESS_TIME ) { Serial.println("A long press is detected"); isLongDetected = true; } } // save the the last state lastState = currentState; }

Quick Steps

  • Upload the above code to Arduino via Arduino IDE
  • Long and short press the button.
  • See the result on Serial Monitor

※ NOTE THAT:

The Serial Monitor may show several short press detection when long press. This is the normal behavior of the button. This behavior is called the “chattering phenomenon”. The issue will be solved in the last part of this tutorial.

Long Press and Short Press with Debouncing

It is very important to debounce the button in many applications.

Debouncing is a little complicated, especially when using multiple buttons. To make it much easier for beginners, we created a library, called ezButton.

We will use this library in below codes

Short Press and Long Press with debouncing after released

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-long-press-short-press */ #include <ezButton.h> const int SHORT_PRESS_TIME = 1000; // 1000 milliseconds const int LONG_PRESS_TIME = 1000; // 1000 milliseconds ezButton button(7); // create ezButton object that attach to pin 7; unsigned long pressedTime = 0; unsigned long releasedTime = 0; void setup() { Serial.begin(9600); button.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { button.loop(); // MUST call the loop() function first if(button.isPressed()) pressedTime = millis(); if(button.isReleased()) { releasedTime = millis(); long pressDuration = releasedTime - pressedTime; if( pressDuration < SHORT_PRESS_TIME ) Serial.println("A short press is detected"); if( pressDuration > LONG_PRESS_TIME ) Serial.println("A long press is detected"); } }

Quick Steps

  • Install ezButton library. See How To
  • Upload the above code to Arduino via Arduino IDE
  • Long and short press the button.
  • See the result on Serial Monitor

Short Press and Long Press with debouncing During Pressing

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-long-press-short-press */ #include <ezButton.h> const int SHORT_PRESS_TIME = 1000; // 1000 milliseconds const int LONG_PRESS_TIME = 1000; // 1000 milliseconds ezButton button(7); // create ezButton object that attach to pin 7; unsigned long pressedTime = 0; unsigned long releasedTime = 0; bool isPressing = false; bool isLongDetected = false; void setup() { Serial.begin(9600); button.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { button.loop(); // MUST call the loop() function first if(button.isPressed()){ pressedTime = millis(); isPressing = true; isLongDetected = false; } if(button.isReleased()) { isPressing = false; releasedTime = millis(); long pressDuration = releasedTime - pressedTime; if( pressDuration < SHORT_PRESS_TIME ) Serial.println("A short press is detected"); } if(isPressing == true && isLongDetected == false) { long pressDuration = millis() - pressedTime; if( pressDuration > LONG_PRESS_TIME ) { Serial.println("A long press is detected"); isLongDetected = true; } } }

Quick Steps

  • Install ezButton library. See How To
  • Upload the above code to Arduino via Arduino IDE
  • Long and short press the button.
  • See the result on Serial Monitor

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Why Needs Long Press and Short Press

  • To save the number of buttons. A single button can keep two or more functionalities. For example, short press for changing operation mode, long press for turn off the device.
  • Use of long press to reduce the short press by accident. For example, some kinds of devices use the button for factory reset. If the button is pressed by accident, it is dangerous. To avoid it, the device is implemented to be factory reset only when the button is long-press (e.g over 5 seconds)

The Best Arduino Starter Kit

※ OUR MESSAGES