Adding potentiometer after a toggle push button

oh sorry about that, in the second caption I’m asking if "at that point, can I just push the button and make it turn off ? instead of activating the power first by pushing the button?” so basically, how can I turn off the LED by just pushing the button?

Hope this make sense :smile:

Sorry about that, in the second caption I’m asking if "at that point, can I just push the button and make it turn off ? instead of activating the power first by pushing the button?” so basically, how can I turn off the LED by just pushing the button?

It's not clear what "at that point" actually is.
The video doesn't really help explain what you want to do.

Hi, so basically, how can I turn off the LED by just pushing the button?

I think you are trying to do too many things with just one button. You want it to enable/disable the pot and turn off/on an LED.
Does the LED go on when the pot is enabled and off when the pot is disabled?

That's new...

give it a try and show us your attempt... you can use one of the numerous button library such as Button in easyRun or OneButton or Toggle or EasyButton or Bounce2, ...

yes it does

You have 3 LEDs
Which one is controlled by the pot?
Which one goes off/on?
What does the third one do?

all of them are controlled by the pot

all of them goes on and off if I use the pot

Post the code that you have now that is working.

In the IDE, click on Edit, then Copy for Forum, that will copy your code for this forum.
Then come back here and just do a simple paste.

You should see the code in a special box with scroll bars

to answer the question you deleted about why I use const byte instead of int for pin definition :

that's because a PIN number is a constant (hence the const), it's less than 255 and always positive, so an unsigned 8 bit variable will do ➜ that's what byte is.

Using an int means you could by mistake change it later in the code as it's not constant (the compiler won't bark at you if you do) and it will use more bytes than necessary (even though it's likely the optimizer will deal with that for you).


```cpp
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);

  // not necessary, they will be LOW by default
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, 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.
    analogWrite(ledPin2, brightness);
    analogWrite(ledPin3, brightness);
  }
}
```

this is the code is use now

Does not compile
Are you sure you posted the right code?
Are you using an Uno?
Are you using the Arduino IDE?

My mistake

no worries! :smile:

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(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);  
    analogWrite(ledPin2, brightness);
    analogWrite(ledPin3, brightness);
  }
  else { // Jim added this
    // Turn LED off
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
  }
}

OH MY GOSH thank you so much Jim, it WORKS!!!

May I know what did you add or change?

I'm seeing this in the pasted code

I would be you used AI to generate the code and did not really understand code tags...

It right there in the code // Jim added this
just the else and setting the pins LOW

It doesn't.

Did you mean that if the pot is off and you push the button and the led will also go off? How will the program know to do that instead of switching the pot on again?

I guess you could use short-clicks and long-clicks?

Hi, it’s the code from PaulRB, I don’t think it’s AI since it seems that he has a lot experience with coding. Also, I think the cpp came from me copying the IDE

And yes, I didn’t understand the code fully, hence why I seek help here and will ask more questions to understand the code tags :smile: . I have zero experience in coding before the last 3 weeks.