// Define pin numbers for LEDs
const int ledPins[] = {2, 4, 16, 17, 5};
// Define pin numbers for switches
const int switchPins[] = {18, 19, 21};
// Variables to store switch states
int switchStates[3] = {0, 0, 0};
// Mode variable
int mode = 1;
// Setup function
void setup() {
// Initialize LED pins as outputs
for (int i = 0; i < 5; i++) {
pinMode(ledPins[i], OUTPUT);
}
// Initialize switch pins as inputs with internal pull-up resistors
for (int i = 0; i < 3; i++) {
pinMode(switchPins[i], INPUT_PULLUP);
}
}
// Function to update the mode based on switch states
void updateMode() {
switchStates[0] = digitalRead(switchPins[0]);
switchStates[1] = digitalRead(switchPins[1]);
switchStates[2] = digitalRead(switchPins[2]);
if (switchStates[0] == LOW) {
mode = 1;
} else if (switchStates[1] == LOW) {
mode = 2;
} else if (switchStates[2] == LOW) {
mode = 3;
}
}
// Function for Mode 1: All LEDs ON
void mode1() {
for (int i = 0; i < 5; i++) {
digitalWrite(ledPins[i], HIGH);
}
}
// Function for Mode 2: All LEDs OFF
void mode2() {
for (int i = 0; i < 5; i++) {
digitalWrite(ledPins[i], LOW);
}
}
// Function for Mode 3: Running lights
void mode3() {
static int currentLed = 0;
static unsigned long lastUpdateTime = 0;
const unsigned long interval = 200; // Time interval between steps in milliseconds
if (millis() - lastUpdateTime >= interval) {
lastUpdateTime = millis();
// Turn off all LEDs
for (int i = 0; i < 5; i++) {
digitalWrite(ledPins[i], LOW);
}
// Turn on the current LED
digitalWrite(ledPins[currentLed], HIGH);
// Move to the next LED
currentLed = (currentLed + 1) % 5;
}
}
// Loop function
void loop() {
// Update mode based on switch input
updateMode();
// Execute the corresponding mode function
switch (mode) {
case 1:
mode1();
break;
case 2:
mode2();
break;
case 3:
mode3();
break;
}
// Add a small delay to avoid bouncing issues
delay(50);
}