Can I use a button to reset the potentiometer?

I am curious whether I can add a button to reset the value of the potentiometer.

I set up a potentiometer to control the light level of the LED. On the other hand, I added another button to turn the LED to switch on and off the light. I tried many times. However, it seems the value keeps referring to the potentiometer. May I ask whether anyone knows how I can fix it? Thank you.

My code is like this:

//For dimmable orange bulb
int Potent = 0;
int pwm = 0;

//For Master Button
int buttonStateAll = 0;
int stateAll = 0;
int sensorValue;

void setup()
{
  pinMode(10, OUTPUT);
  pinMode(4, INPUT);
  pinMode(A0, INPUT);
  Serial.begin(9600);
}

void loop()
{
  //Control the dimmable LED
  Potent = analogRead(A0);
  pwm = map(Potent, 0, 1023, 0, 255);
  analogWrite(10, pwm);
  
  //Master Control 
  buttonStateAll = digitalRead(4);
  if (digitalRead(4) == HIGH)
    {
      if (stateAll == 0)
      {
        analogWrite(10, 0);
        stateAll += 1;
      }
      else
      { 
        analogWrite(10, 255);
        stateAll = 0;
      }
    }
}

You still have the potentiometer code in the loop.

So, even if you reset the value with the button, on the next loop the dimmer code will execute again and adjust the brightness based on the value from the potentiometer.

1 Like

If you want an on/off type dimmer switch then only check then potentiometer value when the light on "ON".

1 Like

If you replace your potentiometer with a rotary encoder, then you can use a button to reset the numbers the rotary encoder produces.

However, be warned that a rotary encoder is a bit more complex to use than a simple pot. I would advise using the Rotary Library
from Encoder Library, for Measuring Quadarature Encoded Position or Rotation Signals
And read the instructions that come with that library.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.