#include <Arduino.h>
struct Button {
const uint8_t PIN;
uint32_t numberKeyPresses;
bool pressed;
};
Button button1 = {13, 0, false};
Button button2 = {18, 0, false};
void ARDUINO_ISR_ATTR isr1() {
// button1.numberKeyPresses += 1;
// button1.pressed = true;
static uint32_t lastMillis;
if ((millis() - lastMillis) > 250) { // Software debouncing buton
button1.numberKeyPresses += 1;
button1.pressed = true;
}
lastMillis = millis();
}
void ARDUINO_ISR_ATTR isr2() {
// button2.numberKeyPresses += 1;
// button2.pressed = true;
static uint32_t lastMillis;
if ((millis() - lastMillis) > 1500) { // Software debouncing buton
button2.numberKeyPresses += 1;
button2.pressed = true;
}
lastMillis = millis();
}
void setup() {
Serial.begin(115200);
pinMode(button1.PIN, INPUT_PULLUP);
attachInterrupt(button1.PIN, isr1, FALLING);
pinMode(button2.PIN, INPUT_PULLUP);
attachInterrupt(button2.PIN, isr2, FALLING);
// attachInterrupt(digitalPinToInterrupt(button2.PIN), isr, FALLING); // copilot
}
void loop() {
// enable, disable interrupt
// if (button1.pressed) { //
// detachInterrupt(button1.PIN);// disable interrupt
// Serial.printf("Button 1 : %lu times\n", button1.numberKeyPresses);
// delay(20);
// button1.pressed = false;
// attachInterrupt(button1.PIN, isr1, FALLING); // attach interrupt
// }
if (button1.pressed) {
button1.pressed = false;
Serial.printf("Button 1 : %lu times\n", button1.numberKeyPresses);
}
if (button2.pressed) {
Serial.printf("Button 2 : %lu times\n", button2.numberKeyPresses);
button2.pressed = false;
}
/*
static uint32_t lastMillis = 0;
if (millis() - lastMillis > 10000) {
lastMillis = millis();
detachInterrupt(button1.PIN);
}
*/
}