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

Hi! Can anyone help me with the coding?

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.

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.

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:

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

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

Good Luck!

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)

// 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).

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?

  • Are you from the same class as this person ? :thinking:

yes I am! lol

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

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).

  • Who made the wiring diagram ?
  • Where did you get your code from ?
  • In a few sentences, please tell us exactly what you decided you want the components to do.

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

  • 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 ?

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.

  • 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 ?

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.

Why not copy his code.

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

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.

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!

  • 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.

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