// Define the LED and switch pins
int ledPin = 3;
int switchPin = 2;
bool buttonPressed = false;
void setup() {
// Initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
// Initialize the switch pin as an input with internal pull-up resistor
pinMode(switchPin, INPUT_PULLUP);
// Start the Serial communication
Serial.begin(9600);
}
void loop() {
// Read the state of the switch
int switchState = digitalRead(switchPin);
// Check if the button was just pressed
if (switchState == LOW && !buttonPressed) {
buttonPressed = true;
digitalWrite(ledPin, LOW); // Turn off the LED
}
// Check if the button is released
if (switchState == HIGH) {
buttonPressed = false;
}
// If the button is not pressed, blink the LED
if (!buttonPressed) {
digitalWrite(ledPin, HIGH);
delay(500); // Blink delay
digitalWrite(ledPin, LOW);
delay(500); // Blink delay
}
}