Problem using buttons with interrupts

Hi!
I am new on this forum and on programmation on Arduino so I hope I will post correctly my first post.

I am experiencing an issue I can't understand using interrupts on my ESP32 TTGO Display board
basically, I would like to program a simple access to the buttons of my board using interrupts.

First of all, the debounce method does not work cause sometimes I get double prints and second, I get everytime the "millis()" time which is printed and not my "t2-t1" result.

Do anybody knows why?
:confused:

#define BUTTON_A_PIN  0
#define BUTTON_B_PIN  35
#define DEBOUNCE_TIME 250
/////////////////////////////////////////////////////////////////

void buttonAPressed();
void buttonAReleased();
uint32_t pressedTime = 0, t1 = 0, t2 = 0;
/////////////////////////////////////////////////////////////////

void setup() {
  Serial.begin(115200);
  delay(50);
  Serial.println("\n\nTTGO menu");

  // Add external input on button A & B
  pinMode(BUTTON_A_PIN, INPUT_PULLDOWN);
  pinMode(BUTTON_B_PIN, INPUT_PULLDOWN);
  attachInterrupt(BUTTON_A_PIN, buttonAPressed, FALLING);
  attachInterrupt(BUTTON_A_PIN, buttonAReleased, RISING);
}
////////////////////////////////////////////////////////////////

void loop() {
}
/////////////////////////////////////////////////////////////////

void buttonAPressed(){
  if(t1 = 0){
    t1 = millis();
  }
}

void buttonAReleased(){
  if(millis()-t1-DEBOUNCE_TIME>=0){
    t2 = millis();
    Serial.printf("You pressed A for %u [ms]\n", t2-t1);
    t2 = 0;
    t1 = 0;
  }
}

Hello andbip,
Welcome
++Karma; // For posting your code correctly on your first post.

Interrupts are the wrong tool for reading buttons. Interrupts are for things that happen on short time scales and have to be dealt with urgently, button presses are no where near this category.

It is very difficult to do proper deboucing when using interrupts for buttons.

Have a read of https://forum.arduino.cc/index.php?topic=711767.0 for more about reading buttons.

As Perry said you do not need to use interrupts to read the buttons. However, I would like to point out a couple of issues:

  1. Comparison is '=='
  if(t1 = 0){
  1. You should NEVER use delay() or Serial functions from an interrupt.

Thanks for your answers. It's now clear I have to do it another way.
I will get a look a your post PerryBebbington or do it with "button2.h" but i thought it was to low for me.

ToddL1962:
As Perry said you do not need to use interrupts to read the buttons. However, I would like to point out a couple of issues:

  1. Comparison is '=='
  if(t1 = 0){
  1. You should NEVER use delay() or Serial functions from an interrupt.
  1. :o :sweat_smile: :slightly_smiling_face: Argh...
  2. The Serial is just for debug purpose, but OK I will make my interrupt as short as possible and avoid stopping my process.

Since ESP32 runs FreeRTOS, button reading / debouncing could be done in a separate task. I'm not saying it SHOULD be done this way, but it COULD be.