// Define constants for LED pins
const int LED_PIN_1 = 5;
const int LED_PIN_2 = 6;
const int LED_PIN_3 = 7;
// Define constants for button pins
const int BUTTON_PIN_1 = 2;
const int BUTTON_PIN_2 = 3;
const int BUTTON_PIN_3 = 4;
// Variables to track active LED
int activeLED = 0;
void setup() {
// Initialize LED pins as outputs
pinMode(LED_PIN_1, OUTPUT);
pinMode(LED_PIN_2, OUTPUT);
pinMode(LED_PIN_3, OUTPUT);
// Initialize button pins as inputs with internal pull-ups
pinMode(BUTTON_PIN_1, INPUT_PULLUP);
pinMode(BUTTON_PIN_2, INPUT_PULLUP);
pinMode(BUTTON_PIN_3, INPUT_PULLUP);
}
void loop() {
// Check button 1 state
if (digitalRead(BUTTON_PIN_1) == LOW) {
// Button 1 pressed, switch active LED to 1 and turn off others
activeLED = 1;
digitalWrite(LED_PIN_2, LOW);
digitalWrite(LED_PIN_3, LOW);
}
// Check button 2 state
else if (digitalRead(BUTTON_PIN_2) == LOW) {
// Button 2 pressed, switch active LED to 2 and turn off others
activeLED = 2;
digitalWrite(LED_PIN_1, LOW);
digitalWrite(LED_PIN_3, LOW);
}
// Check button 3 state
else if (digitalRead(BUTTON_PIN_3) == LOW) {
// Button 3 pressed, switch active LED to 3 and turn off others
activeLED = 3;
digitalWrite(LED_PIN_1, LOW);
digitalWrite(LED_PIN_2, LOW);
}
// Blink the active LED
if (activeLED == 1) {
blinkLED(LED_PIN_1);
} else if (activeLED == 2) {
blinkLED(LED_PIN_2);
} else if (activeLED == 3) {
blinkLED(LED_PIN_3);
}
}
// Function to blink an LED
void blinkLED(int pin) {
digitalWrite(pin, !digitalRead(pin)); // Toggle the LED state
delay(200); // Adjust blink rate as needed
}