# Light Sweep using 5 LEDs, toggle button for ON/OFF, potentiometer for brightness

**URL:** <https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599>\
**Category:** General Guidance\
**Created:** [August 16, 2025, 1:46pm UTC](https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599 "2025-08-16T13:46:47Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![kakoona](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kakoona/32/1172724_2.png) [@kakoona](https://forum.arduino.cc/u/kakoona)\
**Post date:** [August 16, 2025, 1:46pm UTC](https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599/1 "2025-08-16T13:46:47Z")

</div>

Hi! Can anyone help me with the coding?

```arduino
bool btnIsUp = true; // the button is not being pressed at the start
bool lightIsOn = false; // the LEDs are OFF at the start

// constant variables (won't change)
const int ledPin1 = 3; // LED 1 attached to number 3
const int ledPin2 = 5; // LED 2 attached to number 5
const int ledPin3 = 6; // LED 3 attached to number 6
const int ledPin4 = 9; // LED 4 attached to number 9
const int ledPin5 = 10; // LED 5 attached to number 10
const int btnPin = 2; // push button attached to number 2
const int potPin = A0; // potentiometer attached to A0

// LED Array
int timer = 200;
int ledPins[] = { 3, 5, 6, 9, 10 };
int pinTotal = 5;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(btnPin, INPUT_PULLUP);
  pinMode(potPin, INPUT);

  Serial.begin(9600); // default 9600 baud rate

  // Set the LEDs off at the start
  digitalWrite(ledPin1, lightIsOn);
  digitalWrite(ledPin2, lightIsOn);
  digitalWrite(ledPin3, lightIsOn);
  digitalWrite(ledPin4, lightIsOn);
  digitalWrite(ledPin5, lightIsOn);

  // LED Array Output
  for (int thisPin = 0; thisPin < pinTotal; thisPin++) {
    pinMode(ledPins[thisPin], OUTPUT);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(btnPin) == HIGH && btnIsUp) { // if the button is pressed AND the button was UP
    btnIsUp = false; // the button is DOWN now

    if (lightIsOn) { // to check if the LEDs are ON
      lightIsOn = false; // turn the LEDs OFF, if the LEDs are ON
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin3, LOW);
      digitalWrite(ledPin4, LOW);
      digitalWrite(ledPin5, LOW);
      for (int thisPin = 0; thisPin < pinTotal; thisPin++) {
        digitalWrite(ledPins[thisPin], HIGH);
        delay(timer);
        digitalWrite(ledPins[thisPin], LOW);
      }
      for (int thisPin = 0; thisPin < pinTotal; thisPin++) {
        digitalWrite(ledPins[thisPin], HIGH);
        delay(timer);
        digitalWrite(ledPins[thisPin], LOW);
      }
    } else {
      lightIsOn = true; // turn the LEDs ON, if the lightIsOn is false
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin3, HIGH);
      digitalWrite(ledPin4, HIGH);
      digitalWrite(ledPin5, HIGH);
    }
  }

  if (digitalRead(btnPin) == LOW) { // if the button pin is UP
    btnIsUp = true;
  }
}

```

My goal is to make the LEDs turn ON once the button is pressed (the LEDs need to stay ON), then the LEDs will turn OFF when I press the button again.

After I turn ON the LEDs for a few seconds, I want to make a light sweeping effect from LED Pin 1 to 5 where I also be able to adjust the LEDs brightness using the potentiometer.

If it's possible, I also want to use the random() command to make a random light sweep effect, but I don't know how to use it.

I attached my circuit too.

Thank you in advance.

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/7/3/c/73c502e4001bb83dfac16147c387ee7e8dedc379.png)

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [August 16, 2025, 2:12pm UTC](https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599/2 "2025-08-16T14:12:29Z")

</div>

Your topic does not indicate a problem with the _Arduino Command Line Tools_ and therefore has been moved to a more suitable location on the forum.

---

<div class="post-metadata">

**Author:** ![blh64](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blh64/32/605986_2.png) [@blh64](https://forum.arduino.cc/u/blh64)\
**Post date:** [August 16, 2025, 2:52pm UTC](https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599/3 "2025-08-16T14:52:56Z")

</div>

Welcome to the forums! What is your code doing now or not doing now that you want to change? You describe what you want it to do, but now what it is currently doing.

It appears you have mashed together some code since you have all 5 of your leds as single variables (ledPin1, ledPin2, …) as well as an array of the pins `ledPins[]`. You certainly do not need both.

Your logic appears flawed:

> [@kakoona](#):
>
> ```arduino
> if (digitalRead(btnPin) == HIGH && btnIsUp) { // if the button is pressed AND the button was UP
> btnIsUp = false; // the button is DOWN now
> 
> ```

Since your have your button wired between your input pin and ground (GOOD) and you have declared the in as INPUT\_PULLUP, that means the pin will read HIGH when **NOT** pressed and read LOW when pressed. It is the reverse of what most newbies first think it should be, but a very common situation.

I would suggest getting all those things fixed/sorted out first before you start tackling tougher sequences of lights. Once you do, it will be worth your while to study this: [Demonstration code for several things at the same time - General Guidance - Arduino Forum](https://forum.arduino.cc/index.php?topic=223286.0)

It will help you do the patterns while still checking your button.

Good Luck!

---

<div class="post-metadata">

**Author:** ![kakoona](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kakoona/32/1172724_2.png) [@kakoona](https://forum.arduino.cc/u/kakoona)\
**Post date:** [August 16, 2025, 3:09pm UTC](https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599/4 "2025-08-16T15:09:56Z")

</div>

Hi! Thank you for the reply.

Currently, when I use the code, all of the LEDs turn ON at the start before I press the button. Then, when I press the button, the LEDs start doing the LED array 2 times and turn OFF. Then when I press the button again, it turns all the LEDs ON, then repeats the LED array 2 times and turns OFF.

I did test 2 codes to turn and keep the LEDs ON when I press the button, then turn it OFF on the next press.

First Code (works well)

```arduino
// constant variables (won't change)
const int ledPin1 = 3; // LED 1 attached to number 3
const int ledPin2 = 5; // LED 2 attached to number 5
const int ledPin3 = 6; // LED 3 attached to number 6
const int ledPin4 = 9; // LED 4 attached to number 9
const int ledPin5 = 10; // LED 5 attached to number 10
const int btnPin = 2; // push button attached to number 2
const int potPin = A0; // potentiometer attached to A0

// changing variables
int ledState = LOW; // to track the LED state
int btnState; // to track the current button state
int lastbtnState = LOW; // to track the previous button state

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin1, OUTPUT); // set the LED lights as the output (lights = outcome)
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(btnPin, INPUT_PULLUP); // use push button to send signals (ON/OFF), activate the built-in pull-up resistor
  pinMode(potPin, INPUT); // use potentiometer to send signals

  Serial.begin(9600); // default 9600 baud rate

  // Set the LEDs off at the start
  digitalWrite(ledPin1, ledState);
  digitalWrite(ledPin2, ledState);
  digitalWrite(ledPin3, ledState);
  digitalWrite(ledPin4, ledState);
  digitalWrite(ledPin5, ledState);
}

void loop() {
  // put your main code here, to run repeatedly:
  btnState = digitalRead(btnPin); // read the signal from the push button

  if (btnState != lastbtnState) { // The current button state is NOT the last button state (LOW), not pressed yet
    if (btnState == LOW) { // when the button is pressed
      ledState = !ledState; // the LEDs become HIGH (5V)
      digitalWrite(ledPin1, ledState);
      digitalWrite(ledPin2, ledState);
      digitalWrite(ledPin3, ledState);
      digitalWrite(ledPin4, ledState);
      digitalWrite(ledPin5, ledState);
    }
    delay(50); // delay for 50 milliseconds, for debouncing
  }
  lastbtnState = btnState; // the current button state is saved as the last button state
}

```

- I haven’t try to combine it with other code.

Second Code

- Only works one time. The next time I try to upload the same code to the Arduino, the LEDs are ON from the start before I press the button. (I don’t know what’s wrong with this).

- This is the code that I use when I upload this post. I use other code as well to incorporate the LED array (light sweep effect).

```arduino
bool btnIsUp = true; // the button is not being pressed at the start
bool lightIsOn = false; // the LEDs are OFF at the start

// constant variables (won't change)
const int ledPin1 = 3; // LED 1 attached to number 3
const int ledPin2 = 5; // LED 2 attached to number 5
const int ledPin3 = 6; // LED 3 attached to number 6
const int ledPin4 = 9; // LED 4 attached to number 9
const int ledPin5 = 10; // LED 5 attached to number 10
const int btnPin = 2; // push button attached to number 2
const int potPin = A0; // potentiometer attached to A0

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin1, OUTPUT); // set the LED lights as the output (lights = outcome)
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(btnPin, INPUT_PULLUP); // use push button to send signals (ON/OFF), activate the built-in pull-up resistor
  pinMode(potPin, INPUT); // use potentiometer to send signals

  Serial.begin(9600); // default 9600 baud rate

  // Set the LEDs off at the start
  digitalWrite(ledPin1, lightIsOn);
  digitalWrite(ledPin2, lightIsOn);
  digitalWrite(ledPin3, lightIsOn);
  digitalWrite(ledPin4, lightIsOn);
  digitalWrite(ledPin5, lightIsOn);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(btnPin) == HIGH && btnIsUp) { // if the button is pressed AND the button was UP, then it's just been pressed
    btnIsUp = false; // the button is DOWN now, so the button is no longer up

    if (lightIsOn) { // to check if the LEDs are ON
      lightIsOn = false; // turn the LEDs OFF, if the LEDs are ON
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin3, LOW);
      digitalWrite(ledPin4, LOW);
      digitalWrite(ledPin5, LOW);
    } else {
      lightIsOn = true; // turn the LEDs ON, if the lightIsOn is false
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin3, HIGH);
      digitalWrite(ledPin4, HIGH);
      digitalWrite(ledPin5, HIGH);
    }
  }

  if (digitalRead(btnPin) == LOW) { // if the button pin is UP
    btnIsUp = true;
  }
}

```

Does this make sense?

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [August 16, 2025, 3:22pm UTC](https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599/5 "2025-08-16T15:22:10Z")

</div>

- Are you from the same class as this person ? 🤔

> [@Adding potentiometer after a toggle push button](https://forum.arduino.cc/t/adding-potentiometer-after-a-toggle-push-button/1402554):
>
> int potPin = A0; int btnPin = 4; int ledPin1 = 11; int ledPin2 = 6; int ledPin3 = 5; int btnState; int lastbtnState = LOW; int ledState = LOW; void setup() { pinMode(btnPin, INPUT\_PULLUP); pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); digitalWrite(ledPin1, ledState); digitalWrite(ledPin2, ledState); digitalWrite(ledPin3, ledState); } void loop() { int ledVal1 = analogRead(ledPin1); int ledVal2 = analogRead(ledPin2); int ledVal3 = analogRead…

---

<div class="post-metadata">

**Author:** ![kakoona](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kakoona/32/1172724_2.png) [@kakoona](https://forum.arduino.cc/u/kakoona)\
**Post date:** [August 16, 2025, 3:22pm UTC](https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599/6 "2025-08-16T15:22:48Z")

</div>

yes I am! lol

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [August 16, 2025, 3:25pm UTC](https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599/7 "2025-08-16T15:25:59Z")

</div>

- Please show us the exact instructions **you** were given by the teacher.
- Who made the wiring diagram ?
- Where did you get your code from ?

---

<div class="post-metadata">

**Author:** ![kakoona](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kakoona/32/1172724_2.png) [@kakoona](https://forum.arduino.cc/u/kakoona)\
**Post date:** [August 16, 2025, 3:28pm UTC](https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599/8 "2025-08-16T15:28:42Z")

</div>

we’re being asked to use one or more LEDs, a toggle button to turn the LEDs ON/OFF, a potentiometer to adjust the LEDs’ brightness, and to control the LEDs independently (if possible).

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [August 16, 2025, 3:29pm UTC](https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599/9 "2025-08-16T15:29:40Z")

</div>

- Who made the wiring diagram ?
- Where did you get your code from ?

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [August 16, 2025, 3:32pm UTC](https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599/10 "2025-08-16T15:32:30Z")

</div>

> [@kakoona](#):
>
> we’re being asked to use one or more LEDs, a toggle button to turn the LEDs ON/OFF, a potentiometer to adjust the LEDs’ brightness, and to control the LEDs independently (if possible).

- In a few sentences, please tell us **exactly** what **you** decided you want the components to do.

---

<div class="post-metadata">

**Author:** ![kakoona](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kakoona/32/1172724_2.png) [@kakoona](https://forum.arduino.cc/u/kakoona)\
**Post date:** [August 16, 2025, 3:33pm UTC](https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599/11 "2025-08-16T15:33:06Z")

</div>

I made my own wiring diagram. For the code, I found them on YouTube.

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [August 16, 2025, 3:33pm UTC](https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599/12 "2025-08-16T15:33:38Z")

</div>

- In a few sentences, please tell us **exactly** what **you** decided you want the components to do.

- When is the assignment expected to be ready ?

---

<div class="post-metadata">

**Author:** ![kakoona](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kakoona/32/1172724_2.png) [@kakoona](https://forum.arduino.cc/u/kakoona)\
**Post date:** [August 16, 2025, 3:37pm UTC](https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599/13 "2025-08-16T15:37:57Z")

</div>

First, I want all the LEDs to turn ON when I press the button once, where the LEDs need to stay ON (the LEDs doesn’t go OFF when I release the button).

Then, after I turn ON the LEDs for a few seconds, I want to make a light sweeping effect from LED Pin 1 to 5 where I also be able to adjust all the LEDs’ brightness using the potentiometer (while it does the light sweeping / light array effect).

Then the LEDs will turn OFF when I press the button again.

\*This is optional but If it's possible, I also want to use the random() command to make a random light sweep effect, but I don't know how to use it.

It must be ready tomorrow night.

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [August 16, 2025, 3:43pm UTC](https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599/14 "2025-08-16T15:43:20Z")

</div>

- It’s breakfast here right now, I’ll return after eating.

- Other volunteers might step-up and help in the meantime.

- What time of day is it where you are ?

- Do you have the time over the next hour or two to learn things ?

---

<div class="post-metadata">

**Author:** ![kakoona](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kakoona/32/1172724_2.png) [@kakoona](https://forum.arduino.cc/u/kakoona)\
**Post date:** [August 16, 2025, 3:46pm UTC](https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599/15 "2025-08-16T15:46:07Z")

</div>

that’s okay, take your time!

and it’s 2 A.M now here.

I try to learn or tweak my code before I go to sleep.

I think I can stay for the next 30 mins to 1 hour.

It should be done in the next 24 hours then.

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [August 16, 2025, 3:53pm UTC](https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599/16 "2025-08-16T15:53:51Z")

</div>

> [@kakoona](#):
>
> yes I am! lol

Why not copy his code.

> [@Adding potentiometer after a toggle push button](https://forum.arduino.cc/t/adding-potentiometer-after-a-toggle-push-button/1402554/37):
>
> Try this: const byte btnPin = 4; const byte ledPin1 = 11; const byte ledPin2 = 6; const byte ledPin3 = 5; const byte potPin = A0; bool potOn = false; int lastBtn = HIGH; void setup() { pinMode(btnPin, INPUT\_PULLUP); // reading the button will be HIGH if not pressed pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, LOW); } void loop() { int btnNow = digitalRead(btnP…

---

<div class="post-metadata">

**Author:** ![kakoona](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kakoona/32/1172724_2.png) [@kakoona](https://forum.arduino.cc/u/kakoona)\
**Post date:** [August 16, 2025, 3:59pm UTC](https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599/17 "2025-08-16T15:59:56Z")

</div>

We approach the assignment a bit differently, but I’ll give it a try!

---

<div class="post-metadata">

**Author:** ![kakoona](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kakoona/32/1172724_2.png) [@kakoona](https://forum.arduino.cc/u/kakoona)\
**Post date:** [August 16, 2025, 4:19pm UTC](https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599/18 "2025-08-16T16:19:10Z")

</div>

Hi, Jim.

I tried and added more pins to the code.

It does turn ON and OFF the LEDs, but when the potentiometer is turned to the right (which decreases the LEDs’ brightness), the LEDs are a bit jittering/flickering.

```arduino
const byte btnPin = 2;
const byte ledPin1 = 3;
const byte ledPin2 = 5;
const byte ledPin3 = 6;
const byte ledPin4 = 9;
const byte ledPin5 = 10;
const byte potPin = A0;

bool potOn = false;
int lastBtn = HIGH;

void setup() {
  pinMode(btnPin, INPUT_PULLUP); // reading the button will be HIGH if not pressed
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);

  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, LOW);

}

void loop() {

  int btnNow = digitalRead(btnPin);
  if (btnNow == LOW and lastBtn == HIGH) {
    //Button has just been pressed
    potOn = !potOn;
    delay(30); //Wait for any button bouncing to finish
  }
  lastBtn = btnNow; //save button state to compare next time

  if (potOn) {
    int brightness = analogRead(potPin) / 4; // Results in 0 to 255
    analogWrite(ledPin1, brightness);  
    analogWrite(ledPin2, brightness);
    analogWrite(ledPin3, brightness);
    analogWrite(ledPin4, brightness);
    analogWrite(ledPin5, brightness);
  }
  else { // Jim added this
    // Turn LED off
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
    digitalWrite(ledPin5, LOW);
  }
}

```

Do you know why it does that? And by any chance, do you know how to add a light sweeping effect to the code?

Thank you!

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [August 16, 2025, 4:22pm UTC](https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599/19 "2025-08-16T16:22:50Z")

</div>

- We always start with writing a preamble which you have done.

- Next we always make a preliminary schematic, see below, does this make sense to you ?  
You can use KiCAD or EasyEDA for drawing.

 ![2025-08-16_10-19-36](https://europe1.discourse-cdn.com/arduino/original/4X/f/1/b/f1bb3652e9ac6a2e28c25db150a94b9e66de7597.jpeg)

---

<div class="post-metadata">

**Author:** ![kakoona](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kakoona/32/1172724_2.png) [@kakoona](https://forum.arduino.cc/u/kakoona)\
**Post date:** [August 16, 2025, 4:29pm UTC](https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599/20 "2025-08-16T16:29:10Z")

</div>

This is the same, right? Although it’s more confusing to look at..

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/1/d/5/1d521681fb1e8237cd73b09af6ce6b82a807068b.png)

[Next page](https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599.md?page=2)
