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();
while(millis()-startTime<5000){
feedTheDog();
}
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){
feedTheDog();
// 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");
startTime=millis();
while(millis()-startTime<5000){
feedTheDog();
}
Serial.print("restarting");
feedTheDog();
}
void setup() {
Serial.begin(115200);
pinMode(PIN_BUTTON, INPUT_PULLUP);
pinMode(PIN_LIMIT, INPUT);
attachInterrupt(digitalPinToInterrupt(PIN_BUTTON), buttonpressed, FALLING);
// Configure LED output
pinMode(PIN_LED, OUTPUT);
}
void loop() {
digitalWrite(PIN_LED,LOW);
}
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
}