const int ledPin = 4;
const int buttonPin = 8;
int pressCount = 0;
bool isBlinking = false;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("Press, Hold and Release Button (1 click)");
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
int buttonState = digitalRead(buttonPin);
static bool buttonPressed = false;
static unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
if (buttonState == LOW && !buttonPressed && (millis() - lastDebounceTime > debounceDelay)) {
buttonPressed = true;
lastDebounceTime = millis();
pressCount++;
Serial.print("Button is pressed, press count: ");
Serial.println(pressCount);
if (pressCount == 1) {
isBlinking = true;
Serial.println("LED BLINKING");
} else if (pressCount == 4) {
isBlinking = false;
digitalWrite(ledPin, LOW);
Serial.println("LED OFF");
pressCount = 0;
}
}
if (buttonState == HIGH && buttonPressed) {
buttonPressed = false;
Serial.println("Button is released");
}
if (isBlinking) {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
} else {
delay(100);
}
}