// --- English Translation & Fixes by AI ---
// All the pins for the LEDs (total of 6 LEDs)
const uint8_t ledPins[] = { 9, 11, 10, 6, 3, 5 };
const int ledCount = sizeof(ledPins) / sizeof(ledPins[0]);
// The pin for the potentiometer
byte potentiometerPin = A3;
// The pin for the mode switch
byte switchPin = 12;
// The value read from the potentiometer
int potentiometerValue;
// The value read from the switch (HIGH/1 = not pressed, LOW/0 = pressed)
int switchValue;
// Indicates the direction for the chaser effect (-1 for left, 1 for right)
int direction;
// Keeps track of the index of the currently active LED or LED pair
int currentLed = 0; // Start at 0 for cleaner logic
// --- Variables for the "Chaser" effect (Mode 1) only ---
byte trail1 = 0;
byte trail2 = 0;
byte trail3 = 0;
byte trail4 = 0;
// --- Timing variables ---
unsigned long previousMillis = 0;
long interval = 200; // Default interval
void setup() {
// Turn on the serial monitor for debugging
Serial.begin(115200);
// Initialize all LED pins as outputs
for (int i = 0; i < ledCount; i++) {
pinMode(ledPins[i], OUTPUT);
}
// Initialize the switch pin with an internal pull-up resistor.
// This means the pin will be HIGH when the switch is not pressed,
// and LOW when the switch is pressed (connected to ground).
pinMode(switchPin, INPUT_PULLUP);
}
void CollectInput() {
// Read the potentiometer value (0-1023)
potentiometerValue = analogRead(potentiometerPin);
// Read the switch state (HIGH or LOW)
switchValue = digitalRead(switchPin);
}
void Process() {
// --- Mode 1: Chaser with fading tail (Switch is NOT pressed) ---
if (switchValue == 1) {
// Determine direction and speed from potentiometer
direction = 0;
interval = 5000; // Default interval if in the middle "dead zone"
if (potentiometerValue < 482) { // Turning left
direction = -1;
interval = map(potentiometerValue, 482, 0, 500, 75);
}
if (potentiometerValue > 542) { // Turning right
direction = 1;
interval = map(potentiometerValue, 1023, 542, 75, 500);
}
// Update the trail LEDs
trail4 = trail3;
trail3 = trail2;
trail2 = trail1;
trail1 = currentLed;
// Move the leading LED
currentLed += direction;
// Wrap the LED index around if it goes out of bounds
if (currentLed < 0) {
currentLed = ledCount - 1;
}
if (currentLed >= ledCount) {
currentLed = 0;
}
}
// --- Mode 2: "2x2 Burning" Pairs (Switch IS pressed) ---
else { // switchValue == 0
// The potentiometer only controls the speed
interval = map(potentiometerValue, 0, 1023, 500, 75);
// Advance to the next pair of LEDs
currentLed += 2;
// If we've gone past the last pair, wrap around to the first pair (index 0)
if (currentLed >= ledCount) {
currentLed = 0;
}
}
}
void Display() {
// **IMPORTANT**: Turn all LEDs off before lighting the new ones.
// This prevents LEDs from previous frames from staying on.
for (int i = 0; i < ledCount; i++) {
analogWrite(ledPins[i], 0);
}
// --- Display Logic for Mode 1: Chaser ---
if (switchValue == 1) {
// Light the leading LED at full brightness
analogWrite(ledPins[currentLed], 255);
// Light the trailing LEDs with fading brightness
analogWrite(ledPins[trail1], 117);
analogWrite(ledPins[trail2], 54);
analogWrite(ledPins[trail3], 25);
// trail4 is now off because it was pushed out
}
// --- Display Logic for Mode 2: Pairs ---
else { // switchValue == 0
// The desired effect: light up LEDs in pairs.
// Example: 1&2 ON, then 3&4 ON, then 5&6 ON.
// Our 'ledPins' array is 0-indexed, so pairs are (0,1), (2,3), (4,5).
// Light the current pair at full brightness
analogWrite(ledPins[currentLed], 255);
analogWrite(ledPins[currentLed + 1], 255);
}
}
// Timer function remains the same
bool timer() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
return true;
}
return false;
}
void loop() {
// Always check for input
CollectInput();
// Only process and display a new frame when the timer is ready
if (timer()) {
Process();
Display();
// Debugging output to see what's happening
Serial.print("Mode: ");
Serial.print(switchValue == 1 ? "Chaser" : "Pairs");
Serial.print(" | currentLed: ");
Serial.print(currentLed);
Serial.print(" | Interval: ");
Serial.println(interval);
}
}