
Video
Circuit Diagram

Source Code
1// ─────────────────────────────────────────
2// BlueGrays · bluegrays.com
3// Knight Rider LED Effect
4// ─────────────────────────────────────────
5// The 6 LEDs light up one by one forward, then backward (Knight Rider
6// effect). Shows bidirectional for loop logic.
7//
8// Pins:
9// D2, D3, D4, D5, D6, D7 -> 6 LEDs (in order)
10// Each LED with a 330 ohm resistor
11
12int ledPins[] = {2, 3, 4, 5, 6, 7}; // LED pins in lighting order
13
14void setup() {
15 // Set all LED pins as outputs
16 for(int i = 0; i < 6; i++) {
17 pinMode(ledPins[i], OUTPUT);
18 }
19}
20
21void loop() {
22 // Forward: D2 to D7, 100 ms per LED
23 for(int i = 0; i < 6; i++) {
24 digitalWrite(ledPins[i], HIGH);
25 delay(100);
26 digitalWrite(ledPins[i], LOW);
27 }
28
29 // Backward: D6 to D3. The end LEDs are skipped so the bounce looks seamless
30 for(int i = 4; i > 0; i--) {
31 digitalWrite(ledPins[i], HIGH);
32 delay(100);
33 digitalWrite(ledPins[i], LOW);
34 }
35}
36FAQ
How is the "Karaşimşek" (Knight Rider) effect created?▾
The LEDs light up sequentially, first from left to right, then from right to left. Two "for" loops run one after the other; since the second loop counts in the opposite direction, the light moves back and forth.
The LEDs at the ends seem to be flashing twice—how do I fix this?▾
If the loop turns the LED at the end back on, that LED will stay on for two steps. You can prevent repetition at the ends by starting the loop one index number earlier or later (e.g., from 4 to 1).
How do you adjust the speed?▾
Change the delay() value between each step. A value between 30 and 80 ms produces a smooth effect. If you want to adjust the speed while the program is running, you can add a potentiometer, just like in the next project, "Speed-Adjustable Black Lightning."
Can I add more LEDs?▾
Yes, all you need to do is add new pins to the pin array and update the loop limits. The Uno has 12 free digital pins; for more, you can drive 8+ LEDs using just 3 pins with a 74HC595 shift register.
