Adding potentiometer after a toggle push button

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(ledPin3);

  int mappedVal1 = map(ledVal1, 140, 0, 0, 200);
  int mappedVal3 = map(ledVal3, 140, 0, 0, 200);
  Serial.println(mappedVal1);
  Serial.println(mappedVal3);

  btnState = digitalRead(btnPin);

  if (btnState != lastbtnState) {
    if (btnState == LOW) {
      ledState = !ledState;
      digitalWrite(mappedVal1, ledState);
      digitalWrite(ledPin2, ledState);
      digitalWrite(mappedVal3, ledState);
    }
    delay(50);
  }
  lastbtnState = btnState;
}

**Please help, how do I add a potentiometer and limit the first (ledPin1) and third (ledPin3) brightness?

thank you in advance**

int ledVal1 = analogRead(potPin) / 4;  //Results in 0 to 255
. . . 
analogWrite(ledPin1, ledVal1);  
//ledPin1 gets a value of 0 to 255 
//0 = OFF 
//255 = ON  
//127 = half brightness 

Why did you change your initial post after an answer has been made ?

The first code was actually almost fine - adding @LarryD suggestion would be all you need.

Is that school work?

Thank you for your respond :smile: Here's how my circuit look like. I’ve tried your code but the button doesn’t control the potentiometer. Is there anyway I could use the button to turn on the whole system, and have the potentiometer to control all the LEDs?

yes it is :smile:

I didn't change anything. I didn’t know that the pencil symbol is for editing the post, I though someone was writing a reply, lol my bad

Have the button press detection code in your setup and wait for press . The code will be the stuck there until you press. Once you press the setup() terminates and the loop starts running and then in the loop use the code from @LarryD to read the pot and use PWM (analogWrite()) to set the led brightness

What does this code actually do?

Using digitalRead() works on a pin which is in OUTPUT mode. It reads back the last HIGH/LOW value written to the pin with digitalWrite().

Is the same true with analogRead() and PWM pins? Does it read back the last value written to the pin with analogWrite()?

In this case, analogWrite() isn't used in the sketch anyway. The sketch seems very confused!

If you click on the pencil at the top right hand side of post #1, you will be taken to the edit history.

The editing was done by ukhelibob (a moderator) who simply moved the post to a more appropriate category.

1 Like

my bad. sorry for asking - I had the impression the code had changed

May be this will give you some ideas

const byte btnPin  =  4;
const byte ledPin1 = 11;
const byte potPin  = A0;

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

  // not necessary, they will be LOW by default
  digitalWrite(ledPin1, LOW);

  // busy loop waiting for button press
  while (digitalRead(btnPin) == HIGH) yield(); 
}

void loop() {
  int brightness = analogRead(potPin) / 4;  // Results in 0 to 255 
  analogWrite(ledPin1, brightness);  // works because ledPin1 is a PWM pin. Not all are.
}

it is very confusing and I am confused, I’ve just learned coding for 3 weeks

I’ve tried your code, but I could control the potentiometer only if I press the button. My idea is to be able to press the button and let go (turning it on to “activate” the potentiometer), then use the potentiometer to handle the LEDs, and press the button again to turn it off.

Sorry, I’m genuinely confused about this

const byte btnPin  =  4;
const byte ledPin1 = 11;
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);

  // not necessary, they will be LOW by default
  digitalWrite(ledPin1, 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);  // works because ledPin1 is a PWM pin. Not all are.
  }
}

Hope this works how you want. If so, please ask about any lines of code you can't understand. If not, describe what happens in detail and what you expected to happen.

Hi Paul,

I tried your code, but it keep on saying “Compilation error: exit status 1”. Any idea why this happen?

Yes, you only read the last message which says "there was a problem with the code". You didn't read the previous message(s) which would tell what the problem was.

Please always use code tags when you post error messages in your form posts.

I did make some errors!

const byte btnPin  =  4;
const byte ledPin1 = 11;
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);

  // not necessary, they will be LOW by default
  digitalWrite(ledPin1, LOW);

}

void loop() {

  int btnNow = digitalRead(btnPin); 
  if (btnNow == LOW and lastBtn == HIGH) {
    //Button has just been pressed
    potOn = not 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);  // works because ledPin1 is a PWM pin. Not all are.
  }
}

Do you still need help?

Adobe Express - 20250816_222148_1 It works, I finally made a progress thanks to you. Though, I do have a question. Please watch the video, since I cannot explain it (sorry…)

Hi Jim, yes I do :smile:

I watched it. The captions are not readable. What is the question?

Well I see you have it working.
I cannot figure out what you question is from watching the video.