// Define the pin numbers for the two LEDs
const int ledPin1 = 8; // Pin 8 is connected to the first LED
const int ledPin2 = 7; // Pin 7 is connected to the second LED
const int buttonPin = 2; // Pin 2 is connected to the push button
// Variable to store the state of the push button
int buttonState = 1;
// Setup function runs once when the Arduino starts up
void setup() {
// Initialize the digital pins as outputs
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// Initialize the digital pin for the push button as input
pinMode(buttonPin, INPUT);
}
// Loop function runs repeatedly
void loop() {
// Read the state of the push button
buttonState = digitalRead(buttonPin);
// If the push button is pressed, alternate the LEDs
if (buttonState == LOW) {
// Turn on the first LED and turn off the second LED
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
delay(0); // Wait for 500 milliseconds
} else {
// Turn off both LEDs when the button is not pressed
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
}