const int buttonPin = 26; const int ledPin = 27; bool ledBlinking = false; // Flag to track LED blinking state unsigned long lastPressTime = 0; // Timestamp of the last button press unsigned long debounceDelay = 50; // Debounce delay in milliseconds void setup() { pinMode(ledPin, OUTPUT); // Set LED pin as output pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with internal pull-up resistor } void loop() { // the loop function runs over and over again forever static bool lastButtonState = HIGH; // Previous button state bool currentButtonState = digitalRead(buttonPin); // Current button state // Check for button press with debounce, was previously not pressed, and is now pressed if (lastButtonState == HIGH && currentButtonState == LOW && (millis() - lastPressTime) > debounceDelay) { // Ensure debounceDelay time has passed since the last press lastPressTime = millis(); // Update lastPressTime to the current time (from millis()) ledBlinking = !ledBlinking; // Toggle LED blinking state } lastButtonState = currentButtonState; //Update lastButtonState to the current state for the next loop iteration if (ledBlinking) { //If the LED is in blinking mode // Blink LED twice with a 0.5s delay for (int i = 0; i < 2; i++) { // A loop to blink the LED twice digitalWrite(ledPin, HIGH); //Turn ON LED for 0.5 s delay(500); digitalWrite(ledPin, LOW); //Turn Off LED for 0.5 s delay(500); } ledBlinking = false; // Reset after blinking } else { // If ledBlinking is false, the LED remains off digitalWrite(ledPin, LOW); // LED is off when not blinking } }