#define BUTTON_PIN 21 // GPIO21 pin connected to button
#define 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() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
// read the state of the switch/button
currentState = digitalRead(BUTTON_PIN);
if (lastState == HIGH && currentState == LOW) // button is pressed
pressedTime = millis();
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");
lastState = currentState; // save the last state
}