#8Intermediate

Speed Adjustable Led Black Lightning

4.5(59)
115 completed
Speed Adjustable Led Black Lightning
Project with potentiometer that can adjust the speed of the black lightning LED animation in real time. It combines analog input and bidirectional loop control.

Video

Circuit Diagram

Speed Adjustable Led Black Lightning - Circuit

Source Code

Arduino (C++)
1int ledPins[] = {2, 3, 4, 5, 6, 7};
2int potValue;
3
4void setup() {
5  for(int i = 0; i < 6; i++) {
6    pinMode(ledPins[i], OUTPUT);
7  }
8}
9
10void loop() {
11  for(int i = 0; i < 6; i++) {
12    potValue = analogRead(A0);
13    digitalWrite(ledPins[i], HIGH);
14    delay(potValue);
15    digitalWrite(ledPins[i], LOW);
16  }
17
18  for(int i = 4; i > 0; i--) {
19    potValue = analogRead(A0);
20    digitalWrite(ledPins[i], HIGH);
21    delay(potValue);
22    digitalWrite(ledPins[i], LOW);
23  }
24}

FAQ

What does this project teach that is different from previous ones?▾

It combines the analog input (potentiometer) with a bidirectional loop: as the light moves back and forth, the potentiometer is read at each step and updated at the moment of delay. This is the basis for changing behavior based on the sensor value.

The reading is fluctuating—how do I stabilize it?▾

Minor fluctuations in analog readings are normal. Averaging several readings or using the `map()` function to divide the value into coarser intervals (e.g., 10-millisecond intervals) masks the fluctuations.

Can I use a different sensor instead of a potentiometer?▾

Yes. Any sensor that provides an analog output, such as an LDR, can be connected to the same A0 input; for example, as the environment gets darker, the light can change more quickly. You just need to readjust the reading range using `map()`.

Which analog pin should I use?▾

On the Uno, all pins from A0 to A5 can be used for analog readings. Make sure the pin you use in your code matches the connection in your circuit; since A4 and A5 are also used for I2C, choose A0 if you plan to add a display later.

Speed Adjustable Led Black Lightning | BlueGrays