volatile byte state = LOW;
const byte ButtonPin = 2;
const byte led = 13;
const int timeThreshold = 150; //Temps entre interruptions pour eviter bounce
volatile int ISRCounter = 0;
int counter = 0;
long startTime = 0;
void setup() {
// Push button Init
pinMode(led,OUTPUT);
pinMode(ButtonPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ButtonPin), debounce, FALLING);
digitalWrite(led,LOW);
}
void loop() {
//We enter the push button condition
if (counter != ISRCounter){
counter = ISRCounter;
state = !state; //Inverser etat du LED
}
if(state){
hidup();}
else{
mati();}
}
void debounce(){ //Interrupt function to treat push button
if (millis() - startTime > timeThreshold){
ISRCounter++;
startTime = millis();
}
}
void hidup(){
digitalWrite(led,HIGH);
}
void mati(){
digitalWrite(led,LOW);
}