int buttonPin = 2; // Button connected to digital pin 2
int redLedPin = 3; // Red LED connected to digital pin 3
int greenLedPin = 4; // Green LED connected to digital pin 4
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with internal pull-up resistor
pinMode(redLedPin, OUTPUT); // Set red LED pin as output
pinMode(greenLedPin, OUTPUT); // Set green LED pin as output
}
void loop() {
// Read the button state (LOW when pressed, HIGH when not pressed)
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
digitalWrite(redLedPin, LOW); // Turn off the red LED
digitalWrite(greenLedPin, HIGH); // Turn on the green LED
} else {
digitalWrite(redLedPin, HIGH); // Turn on the red LED
digitalWrite(greenLedPin, LOW); // Turn off the green LED
}
}