For how it works, see the comments in the code. the serial output is below the code
struct Button { // allows us to use a few variables in the special isr funciton
const uint8_t PIN; // set the pin number
uint32_t numberKeyPresses; // store the number of key presses
uint32_t totalKeyPresses; // store a sceond variable for key presses
bool pressed; // is button pressed or not ?
};
int bounceLastMillis = 0; // for anti bounce, is it valid press or just noise
int betweenPressMillis = 0; // is not really necessary, for storing more count of press
//Button button1 = {18, 0, false};
Button button1 = { 26, 0, 0, false }; // here is where we pass the variables to the isr function
void IRAM_ATTR DON_isr() { // if you can figure this function out, you are smarter than me.
if (millis() - bounceLastMillis > 10) {
button1.numberKeyPresses += 1;
if (millis() - betweenPressMillis > 1000) {
button1.totalKeyPresses += 1;
}
betweenPressMillis = millis();
button1.pressed = true;
ets_printf("ISR triggered\n"); // a way to print info in the isr function
ets_printf("don ISR trigger '/' n triggered\n");
}
bounceLastMillis = millis();
}
byte highOrLow; // just some var to store a value for printing to test
void setup() {
Serial.begin(115200);
pinMode(button1.PIN, INPUT_PULLUP); // store the pin number and set as input_pullup
// attachInterrupt(button1.PIN, isr, FALLING); // HIGH, LOW CHANGE FALLING RISING
attachInterrupt(button1.PIN, DON_isr, RISING); // attach interupt to pin, run isr function, do it on RISING instead of HIGH, LOW CHANGE FALLING . rising worked best here but depending on what you are trying to do, one of the other options could work better.
}
void loop() { // this just shows that the button press is recognized , but ANYTHINg can happen in the loop as usual and the button press is seen soon as it is pressed without waiting for code in the loop to "see" it
// if (button1.pressed) { // if this line is used nothing happens until button press.
Serial.printf("Button 1 has been pressed %u times\n", button1.numberKeyPresses);
Serial.printf("the number of button presses in group %u \n", button1.totalKeyPresses);
highOrLow = button1.numberKeyPresses;
Serial.print("high or low ");
Serial.println(highOrLow);
button1.pressed = false;
delay(2000);
// }
}
to see the code I used to come up with this see:
https://www.upesy.fr/blogs/tutorials/what-are-interrupts-in-esp32-with-examples-for-arduino-code
and
https://lastminuteengineers.com/handling-esp32-gpio-interrupts-tutorial/
Using esp32, which lets you use attachInterrupt() on every pin, unlike with the Arduino, which depends on the card and only works on some pins. The code can also be a little simpler (not explained here). The isr function capability is truly powerful, particularly when combined with the struct (structure) note the use of the code, IRAM_ATTR DON_isr() that forces RAM to be used instead of flash. DON_isr is just the name of the function, You can name it whatever you want, such as MyIsr or whatever.
A high percentage of this code probably came from someone else. I do not know how to write code, but I can sometimes paste things together that work. Even if the code shown here did not come from one of the sources below, I most likely learned how to make it work with help from them. My apologies to anyone I missed. Please make use of anything you see here, but do not count on good coding practice as I have just done what was needed to make it work. But it does work, unless otherwise noted. Thanks to everyone who has shared their knowledge and good luck to those who are learning. https://www.jarutex.com/index.php/2021/12/19/8868/ https://www.arduino.cc/ https://www.sparkfun.com/ https://randomnerdtutorials.com/ https://youtu.be/3BWSSpcn95A https://stackoverflow.com/ https://youtu.be/684KSAvYbw4 https://exploreembedded.com/ https://www.youtube.com/watch?v=JmvMvIphMnY&t https://prosperityconsulting.com
the number of button presses in group 1
high or low 1
ISR triggered
don ISR trigger ‘/’ n triggered
Button 1 has been pressed 2 times
the number of button presses in group 2
high or low 2
Button 1 has been pressed 2 times
the number of button presses in group 2
high or low 2
ISR triggered
don ISR trigger ‘/’ n triggered
ISR triggered
don ISR trigger ‘/’ n triggered
ISR triggered
don ISR trigger ‘/’ n triggered
ISR triggered
don ISR trigger ‘/’ n triggered
Button 1 has been pressed 6 times
the number of button presses in group 3
high or low 6
Button 1 has been pressed 6 times
the number of button presses in group 3
high or low 6
Button 1 has been pressed 6 times
the number of button presses in group 3
high or low 6
Port closed
Port closed