Skip to content

Commit 50560a2

Browse files
authored
Functional interrupt fix (#8175)
* Added link to external examples to the doc * Fixed FunctionalInterrupt.ino + added introduction comment and few outputs
1 parent 1c3039e commit 50560a2

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

Diff for: libraries/ESP32/examples/GPIO/FunctionalInterrupt/FunctionalInterrupt.ino

+31-8
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,67 @@
1+
/*
2+
* This example demonstrates usage of interrupt by detecting a button press.
3+
*
4+
* Setup: Connect first button between pin defined in BUTTON1 and GND
5+
* Similarly connect second button between pin defined in BUTTON2 and GND.
6+
* If you do not have a button simply connect a wire to those buttons
7+
* - touching GND pin with other end of the wire will behave same as pressing the connected button.
8+
* Wen using the bare wire be careful not to touch any other pin by accident.
9+
*
10+
* Note: There is no de-bounce implemented and the physical connection will normally
11+
* trigger many more button presses than actually happened.
12+
* This is completely normal and is not to be considered a fault.
13+
*/
14+
15+
116
#include <Arduino.h>
217
#include <FunctionalInterrupt.h>
318

419
#define BUTTON1 16
520
#define BUTTON2 17
621

7-
class Button
8-
{
22+
class Button{
923
public:
1024
Button(uint8_t reqPin) : PIN(reqPin){
1125
pinMode(PIN, INPUT_PULLUP);
12-
attachInterrupt(PIN, std::bind(&Button::isr,this), FALLING);
1326
};
14-
~Button() {
27+
28+
void begin(){
29+
attachInterrupt(PIN, std::bind(&Button::isr,this), FALLING);
30+
Serial.printf("Started button interrupt on pin %d\n", PIN);
31+
}
32+
33+
~Button(){
1534
detachInterrupt(PIN);
1635
}
1736

18-
void ARDUINO_ISR_ATTR isr() {
37+
void ARDUINO_ISR_ATTR isr(){
1938
numberKeyPresses += 1;
2039
pressed = true;
2140
}
2241

23-
void checkPressed() {
42+
void checkPressed(){
2443
if (pressed) {
2544
Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses);
2645
pressed = false;
2746
}
2847
}
2948

3049
private:
31-
const uint8_t PIN;
50+
const uint8_t PIN;
3251
volatile uint32_t numberKeyPresses;
3352
volatile bool pressed;
3453
};
3554

3655
Button button1(BUTTON1);
3756
Button button2(BUTTON2);
3857

39-
4058
void setup() {
4159
Serial.begin(115200);
60+
while(!Serial) delay(10);
61+
Serial.println("Starting Functional Interrupt example.");
62+
button1.begin();
63+
button2.begin();
64+
Serial.println("Setup done.");
4265
}
4366

4467
void loop() {

0 commit comments

Comments
 (0)