Page 1 of 1

An unexpected phenomenon about attachInterrupt()

Posted: Fri Apr 29, 2022 8:55 am
by fantasyhpu
Hello, there is a very simple arduino code which using a button attaching esp32 IO21:
[img]C:\Users\admin\Desktop\111.svg[/img]
  1. const int keyPin=21;//change 21 to 2 when using arduino uno
  2. int num=0;
  3. boolean flag=false;
  4.  
  5. void echo()
  6. {
  7.   flag=true;
  8.   num++;
  9. }
  10.  
  11. void setup()
  12. {
  13.   Serial.begin(9600);
  14.   pinMode(keyPin,INPUT);
  15.   attachInterrupt(keyPin,echo,FALLING);//change keyPin to 0 when using arduino uno
  16. }
  17.  
  18. void loop()
  19. {
  20.   if(flag)
  21.   {
  22.     Serial.println(num);
  23.     flag=false;
  24.   }
  25. }
When I clicking the button only once, through the serial port, it was found that the number increased many times. The capacitor using for debounce, and I can be sure that through the oscilloscope, the keys can be significantly debounceed by the capacitor.
It is unexpected the "num" increased many times when clicking the button only once.

At this point, I tested it with arduino uno, it turned out that the "num" had only increased once when clicking the button once. The result is very well when using arduino uno.

Why is the same procedure so different in results? I think it is come from the "attachInterrupt()" in ESP32-arduino. It maybe a bug in ESP32-arduino of attachInterrupt().

Have others found similar problems?

Best regards.

Re: An unexpected phenomenon about attachInterrupt()

Posted: Mon May 02, 2022 1:50 am
by fantasyhpu
The attachment is the figure of circuit.
Image