// Define the total number of LEDs
const int NUM_LEDS = 8;
// Define the pin assignments for the LEDs and buttons
const int LED_pins[] = {2, 3, 4, 5, 6, 7, 8, 9}; // LEDs are connected to digital pins 2 to 9
const int button1_pin = 10; // button 1 is connected to digital pin 10
const int button2_pin = 11; // button 2 is connected to digital pin 11
// Function to activate LEDs sequentially in a leftward direction
void activate_leftward() {
for (int i = 0; i < NUM_LEDS; i++) {
digitalWrite(LED_pins[i], HIGH); // Turn on the current LED
delay(100); // Delay for visualization
digitalWrite(LED_pins[i], LOW); // Turn off the current LED
}
}
// Function to activate LEDs sequentially in a rightward direction
void activate_rightward() {
for (int i = NUM_LEDS - 1; i >= 0; i--) {
digitalWrite(LED_pins[i], HIGH); // Turn on the current LED
delay(100); // Delay for visualization
digitalWrite(LED_pins[i], LOW); // Turn off the current LED
}
}
void setup() {
// Initialize LED pins as output
for (int i = 0; i < NUM_LEDS; i++) {
pinMode(LED_pins[i], OUTPUT);
}
// Initialize button pins as input with internal pull-up resistors enabled
pinMode(button1_pin, INPUT_PULLUP);
pinMode(button2_pin, INPUT_PULLUP);
}
void loop() {
// Check if button 1 is pressed to initiate sequential LED activation in a leftward direction
if (digitalRead(button1_pin) == LOW) {
activate_leftward();
}
// Check if button 2 is pressed to initiate sequential LED activation in a rightward direction
if (digitalRead(button2_pin) == LOW) {
activate_rightward();
}
}