// Define the LED pins
const int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9};
const int numLeds = sizeof(ledPins) / sizeof(ledPins[0]);
// Define the potentiometer pin
const int potPin = A0;
// Variables to store LED index and direction
int ledIndex = 0;
int direction = 1;
void setup() {
// Initialize the LED pins as outputs
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
}
// Initialize the serial communication
Serial.begin(9600);
}
void loop() {
// Read the potentiometer value (0 to 1023)
int potValue = analogRead(potPin);
// Map the potentiometer value to a suitable delay time (10 to 100 milliseconds)
int delayTime = map(potValue, 0, 1023, 10, 100);
// Fade in the current LED and fade out the previous LED
int previousLedIndex = ledIndex - direction;
if (previousLedIndex < 0) {
previousLedIndex = numLeds - 1;
} else if (previousLedIndex >= numLeds) {
previousLedIndex = 0;
}
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(ledPins[ledIndex], brightness);
analogWrite(ledPins[previousLedIndex], 255 - brightness);
delay(delayTime);
}
// Update the LED index and direction
ledIndex += direction;
if (ledIndex == numLeds - 1 || ledIndex == 0) {
direction = -direction; // Reverse direction at the ends
}
}