int buttonPin = 4; // Digital pin where the button is connected.
int ledPin2 = 7;
int ledPin = 8; // Digital pin where the LED is connected.
int buttonState;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // LED pin as an output.
pinMode(ledPin2, OUTPUT); // LED pin as an output.
pinMode(buttonPin, INPUT); // Button pin as an input.
}
void loop() {
// Read the state of the button
buttonState = digitalRead(buttonPin);
// Check if the button is pressed
if (buttonState == 1){
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, LOW);
Serial.println("LED1 ON ");// Turn LED on.
Serial.println("LED2 OFF ");
} else {
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, HIGH);
Serial.println("LED Off ");
Serial.println("LED2 ON ");
}
}