attachInterrupt and/or Serial.print (ESP32)

Hello guys,

I am relatively new to the arduino IDE so please don't be to harsh if it is an obvious mistake.
At the moment I am working with and ESP32 and i am using the Arduino-IDE to program it.

I am trying to generate an interrupt whenever a button is pressed, so that i make sure the button is read in even if i my loop would take a long time. For the interrupt I found attachInterrupt(25, Taster_links_gedruekt, FALLING); which reads in the pin 25 and when it detects a falling edge (button pressed) it calls the function Taster_links_gedruekt.
because of the bouncing of the button I thought it would be an good idea to use an delay function so that the code afterwards will not get executed several times. However this does not really work.

here is the Code i used for testing:

int counter = 0;
void Taster_links_gedruekt() {
delay(1000);
counter ++;

// Serial.println(" links__ " + counter);
Serial.println("12345678987654321 Counter:" + counter);
delay(10);
// timer.disableTimer_Taster_links();
}

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(25, INPUT);
delay(100);
attachInterrupt(25, Taster_links_gedruekt, FALLING);
}

void loop() {
// put your main code here, to run repeatedly:
}

and this is the result i get when pressing the button several times in a shot time frame (higher frequency than 1 Hz):

ets Jun 8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
flash read err, 1000
ets_main.c 371
ets Jun 8 2016 00:22:57

rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:10944
load:0x40080400,len:6388
entry 0x400806b4
2345678987654321 Counter:
345678987654321 Counter:
45678987654321 Counter:
5678987654321 Counter:
678987654321 Counter:
78987654321 Counter:
8987654321 Counter:
987654321 Counter:
87654321 Counter:
7654321 Counter:
654321 Counter:
54321 Counter:
4321 Counter:
321 Counter:
21 Counter:
1 Counter:
Counter:
Counter:
ounter:
unter:
nter:
ter:
er:
r:
:

Any following press of the button does not result in anything (edit: actually it does. After a few empty lines it prints out some voodo shit.

does someone know what is happening here?

edit: The Delay does not appear to do anything. I connected an LED to an Port which I would set on high after the delay u can see above put the LED turns on instantly.

It is always a bad idea to use an interrupt for an ordinary button. Especially if you are a beginner, interrupts are not a good idea at all. A interrupt service routine (ISR) is NOT an ordinary function. There are several restricitions that apply to an ISR - and even more on an ESP32.
ISR's must be short, and there are several functions you must not use in an ISR. 'delay' and Serial.print() belong to these functions. Also an ISR running on ESP32 needs a special attribute IRAM_ATTR, and all functions that are called from within the ISR must also have this attribute.

Check your button in loop, and learn how to program without delay, so that loop loops fast enough for checking the button. And remember that mechanical buttons bounce.

For me, button debounce starts with hardware. I do not use software debounce.

You might find results doing a search using words such as "button debounce circuit"

USE CODE TAGS WHEN POSTING CODE!

You can do so if you want. But debouncing a button by software is fairly easy and standard software programming. Think of a keypad or a button matrix with many buttons. Debouncing this by software is far more easy.

You know that using Serial.prints in a ISR will cause issues, right?

Whiles the interrupt is being processed interrupts are disabled. Serial.print uses interrupts. Eventually there will be a clash.

Instead.

Make a volatile bool variable. Have the ISR set the variable to true when an ISR is done. Only code in ISR.

in loop()

if( isrdiditsthing==true)
do the isr thing set variable back to false.

When delay is active ISR does not work.

Thx guys for the input. I didn't know any of it.
I was afraid that my loop would be taking to long to use the normal method of reading in a button because i am trying to use my ESP32 as a webserver while reading in temperatures. And last time I checkt the loop already took 160ms even without delay(). But I guess I will try to pull it off worst case would be that i would have to press the button longer

Thx again :smiley:

Your posted code does not reflect that.

yeah that's cause that bit of the puzzle works. But thx again.

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