// Pin numbers for the LEDs
const int ledPins[] = {2, 3, 4}; // Define an array to hold the pin numbers of the LEDs
const int numLeds = sizeof(ledPins) / sizeof(ledPins[0]); // Calculate the number of LEDs connected
const int delayTime = 200; // Adjust the delay time for the desired speed
void setup()
{
// Initialize each LED pin as an output
for (int i = 0; i < numLeds; i++)
{
pinMode(ledPins[i], OUTPUT); // Set each pin in the ledPins array as an OUTPUT pin
}
}
void loop() {
// Turn on LEDs from left to right
for (int i = 0; i < numLeds; i++)
{
digitalWrite(ledPins[i], HIGH); // Turn on the LED at the current index position
delay(delayTime); // Wait for delayTime milliseconds before proceeding to the next LED
digitalWrite(ledPins[i], LOW); // Turn off the LED at the current index position
}
// Turn off LEDs from right to left
for (int i = numLeds - 2; i >= 1; i--)
{
digitalWrite(ledPins[i], HIGH); // Turn on the LED at the current index position
delay(delayTime); // Wait for delayTime milliseconds before proceeding to the next LED
digitalWrite(ledPins[i], LOW); // Turn off the LED at the current index position
}
}