// Define the pin numbers
const int switchPin = 2; // the number of the switch pin
const int ledPin = 5; // the number of the LED pin
void setup() {
// Initialize the switch pin as an input with an internal pull-up resistor
pinMode(switchPin, INPUT_PULLUP);
// Initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
// Start with the LED off
digitalWrite(ledPin, LOW);
}
void loop() {
// Read the state of the switch
int switchState = digitalRead(switchPin);
// Check if the switch is pressed
// Since we are using INPUT_PULLUP, the switch will read LOW when pressed
if (switchState == LOW) {
// Turn the LED on
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
} else {
// Turn the LED off
digitalWrite(ledPin, LOW);
}
// Small delay for debouncing the switch
delay(50);
}