An unexpected phenomenon about attachInterrupt()
Posted: Fri Apr 29, 2022 8:55 am
Hello, there is a very simple arduino code which using a button attaching esp32 IO21:
[img]C:\Users\admin\Desktop\111.svg[/img]
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.
[img]C:\Users\admin\Desktop\111.svg[/img]
- const int keyPin=21;//change 21 to 2 when using arduino uno
- int num=0;
- boolean flag=false;
- void echo()
- {
- flag=true;
- num++;
- }
- void setup()
- {
- Serial.begin(9600);
- pinMode(keyPin,INPUT);
- attachInterrupt(keyPin,echo,FALLING);//change keyPin to 0 when using arduino uno
- }
- void loop()
- {
- if(flag)
- {
- Serial.println(num);
- flag=false;
- }
- }
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.