
Video
Circuit Diagram

Source Code
1int ledPins[] = {2, 3, 4, 5, 6, 7};
2
3void setup() {
4 for(int i = 0; i < 6; i++) {
5 pinMode(ledPins[i], OUTPUT);
6 }
7}
8
9void loop() {
10 for(int i = 0; i < 6; i++) {
11 digitalWrite(ledPins[i], HIGH);
12 delay(100);
13 digitalWrite(ledPins[i], LOW);
14 }
15
16 for(int i = 4; i > 0; i--) {
17 digitalWrite(ledPins[i], HIGH);
18 delay(100);
19 digitalWrite(ledPins[i], LOW);
20 }
21}FAQ
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.
