volatile bool flagPlus = false;
volatile bool flagMoins = false;
unsigned long lastInterruptTimePlus = 0;
unsigned long lastInterruptTimeMoins = 0;
int compteur = 0;
const unsigned long debounceDelay = 150; // délai anti-rebond en ms
void setup() {
Serial.begin(9600);
pinMode(2, INPUT_PULLUP); // Bouton +
pinMode(3, INPUT_PULLUP); // Bouton -
attachInterrupt(digitalPinToInterrupt(2), ISR_plus, FALLING); // détection sur appui
attachInterrupt(digitalPinToInterrupt(3), ISR_moins, FALLING);
}
void loop() {
if (flagPlus) {
flagPlus = false;
compteur++;
Serial.print("Compteur : ");
Serial.println(compteur);
}
if (flagMoins) {
flagMoins = false;
compteur--;
Serial.print("Compteur : ");
Serial.println(compteur);
}
}
void ISR_plus() {
unsigned long currentTime = millis();
if (currentTime - lastInterruptTimePlus > debounceDelay) {
flagPlus = true;
lastInterruptTimePlus = currentTime;
}
}
void ISR_moins() {
unsigned long currentTime = millis();
if (currentTime - lastInterruptTimeMoins > debounceDelay) {
flagMoins = true;
lastInterruptTimeMoins = currentTime;
}
}