int ledPins[] = {0, 3, 5, 6, 9, 10, 11};
int carPins[] = {1, 2, 4, 7, 8, 12, 13};
int numLeds = 7;
int numCars = 7;
int delayTime = 100; // Initial delay time in milliseconds
int carCount = 0;
//int direction = 1; // 1 for forward, -1 for reverse
int currentLed = 0; // Current LED index
int intensity = 0; // PWM intensity (0 to 255)
void setup() {
// Set PWM pins as OUTPUT
for (int i = 0; i < numCars; i++) {
pinMode(ledPins[i], OUTPUT);
pinMode(carPins[i], OUTPUT);
}
}
void loop() {
// Turn off all LEDs
for (int i = 0; i < numLeds; i++) {
analogWrite(ledPins[i], 0);
analogWrite(carPins[i], 0);
}
// Set the intensity of the current LED
analogWrite(carPins[carCount], intensity);
if (carCount % 2 == 0) {
analogWrite(ledPins[currentLed], intensity);
currentLed += 1;
if (currentLed == numLeds) {
currentLed = 0;
}
}
carCount += 1;
if (carCount == numCars) {
carCount = 0;
}
/** Reverse direction when reaching the end LEDs
if (currentLed == numLeds || currentLed == -1) {
direction = -direction;
currentLed += 2 * direction;
}
**/
// Adjust the intensity
intensity += 1 * 10; // Change intensity by 10 each step
if (intensity < 0) {
intensity = 0;
}
if (intensity > 255) {
intensity = 255;
}
// Delay to control the rhythm
delay(delayTime);
// Change the delay time for variation
delayTime = map(analogRead(A0), 0, 1023, 50, 300); // Adjust rhythm with a potentiometer on A0
}