Functional interrupts on nano esp32

I'm new to Arduino and trying to figure out my nano esp 32.
I have the following sketch from the examples/esp32/GPO/FunctionalInterrupt.ino

#include <Arduino.h>
#include <FunctionalInterrupt.h>

#define BUTTON1 17
#define BUTTON2 18

class Button{
public:
  Button(uint8_t reqPin) : PIN(reqPin){
    pinMode(PIN, INPUT_PULLUP);
  };

  void begin(){
    attachInterrupt(PIN, std::bind(&Button::isr,this), FALLING);
    Serial.printf("Started button interrupt on pin %d\n", PIN);
  }

  ~Button(){
    detachInterrupt(PIN);
  }

  void ARDUINO_ISR_ATTR isr(){
    numberKeyPresses += 1;
    pressed = true;
  }

  void checkPressed(){
    if (pressed) {
      Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses);
      pressed = false;
    }
  }

private:
    const uint8_t PIN;
    volatile uint32_t numberKeyPresses;
    volatile bool pressed;
};

Button button1(BUTTON1);
Button button2(BUTTON2);

void setup() {
    Serial.begin(9600);
    while(!Serial) delay(10);
    Serial.println("Starting Functional Interrupt example.");
    button1.begin();
    button2.begin();
    Serial.println("Setup done.");
}

void loop() {
  button1.checkPressed();
  button2.checkPressed();
}

The comments in the sketch (omitted here for brevity) say to place a pushbutton between the pin and ground.

I run the sketch and see the initialization messages on the serial console, but no matter which pin I ground, the interrupts are not triggered.

I have tried both pin numbering schemes in the IDE, and have tried changing the BUTTON1 and BUTTON2 constants, but to no avail.

It's not obvious to me what might be wrong and I was hoping someone might point out my error.

Any help you may be able to offer would be gratefully received.

Many thanks.

Mike

Ok, I was scratching my head about how that might work.

What I tried, and what worked, was connecting the GPIO pins through 220ohm resistors to 3.3v and one side of the switches, and the other side of the switches to ground.

That makes a bit more sense to me, and works.

Regards,

Mike

1 Like

Check your physical pins on your board. Ensure that it is connected correctly and the tip of the wire is tight.
Your code uses FALLING mode, the interrupt triggers when the button goes from HIGH to LOW.
I hope it helps.

1 Like

Would be glad to see your application as three files version -- a format that I usually follow to develop class based sketch.

*.ino
*.h
*.cpp
1 Like