// https://roboticsbackend.com/arduino-turn-led-on-and-off-with-button/
#define LED_PIN 2
#define BUTTON_PIN 3
byte lastButtonState = HIGH;
//byte ledState = LOW;
unsigned long debounceDuration = 50; // millis
unsigned long lastTimeButtonStateChanged = 0;
int a = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - lastTimeButtonStateChanged >= debounceDuration) {
byte buttonState = digitalRead(BUTTON_PIN);
if (buttonState != lastButtonState) {
lastTimeButtonStateChanged = currentMillis;
lastButtonState = buttonState;
if (buttonState == HIGH) {
//Serial.println("======= Release =======");
Serial.println("======= Press =======");
a=a+1;
if(a == 1){
digitalWrite(LED_PIN, HIGH);
}else if (a == 2){
digitalWrite(LED_PIN, LOW);
a=0;
}
}
}
}
}