Clear interrupt flag using software

Hello, so i'm writing a simple code on my esp32 that has an interrupt. when interrupt is triggered on pin 21 it will wait 5 seconds then the code reads from the same pin a long press and exits the interrupt. One problem is that when the same pin is long pressed the interrupt is enabled again. I want to clear that interrupt flag before leaving the function so that it would not enter the function again. How can I do this? any help please?
(PS: I tried detach and attach interrupt but that did not work)

Thank you in advance

#include <Arduino.h>
// Pin to which the button, PIR motion detector or radar is connected
#define PIN_BUTTON 21
#define PIN_LED 16
#define PIN_LIMIT 22
#include "soc/timer_group_struct.h"
#include "soc/timer_group_reg.h"

const int LONG_PRESS_TIME = 1000; // 1000 milliseconds

// Uncomment to not put the function in the RAM of the ESP32
//void buttonpressed() {
// The function is placed in the RAM of the ESP32.
void IRAM_ATTR buttonpressed() {
Serial.println("in interrupt");
int startTime=millis();
Serial.print("you may long press");
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;
while (!isLongDetected){
// read the state of the switch/button:
currentState = digitalRead(PIN_BUTTON);

  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;
Serial.println("leaving the interrupt in 5 secs");


void setup() {
attachInterrupt(digitalPinToInterrupt(PIN_BUTTON), buttonpressed, FALLING);

// Configure LED output

void loop() {

void feedTheDog(){
// feed dog 0
TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE; // write enable
TIMERG0.wdt_feed=1; // feed dog
TIMERG0.wdt_wprotect=0; // write protect
// feed dog 1
TIMERG1.wdt_wprotect=TIMG_WDT_WKEY_VALUE; // write enable
TIMERG1.wdt_feed=1; // feed dog
TIMERG1.wdt_wprotect=0; // write protect

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation of your ask).

My first thought is why is an interrupt needed? It is generally not good practice to have delays etc in an interrupt.

Also know that while in an interrupt no other interrupts can occur so I don't believe milli()s are useable in an interrupt.

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