Combining Button with Potentiometer

So i'm running into a little problem here.
I've to connect the potentiometer with the button and a led.
The Led turns on when pressing the button, and the LED should turn of when the potentiometer is at percentage 50.

What happens for me, is that it does work but only when I press the button again.

const int BUTTONLEFT = 9;
const int YELLOWLED = 7;
const int POTPIN = A0;

int LastButtonLeft = HIGH;
int val = 0;

void setup() {
  pinMode(BUTTONLEFT, INPUT_PULLUP);
  pinMode (POTPIN, INPUT_PULLUP);
  pinMode(YELLOWLED, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  int val = analogRead(POTPIN);
  float percentage = map(val, 0, 1023, 0, 100);

  int ButtonLeftState;
  ButtonLeftState = digitalRead(BUTTONLEFT);
  if (ButtonLeftState != LastButtonLeft) { // debounce the input button
    delay(50);
    ButtonLeftState = digitalRead(BUTTONLEFT);
    if (ButtonLeftState == LOW) { 
      digitalWrite (YELLOWLED, HIGH);
      if (percentage <= 50) {
        digitalWrite (YELLOWLED, HIGH);
      } else if (percentage >= 50) {
        digitalWrite (YELLOWLED, LOW);
      }
    }
    LastButtonLeft = ButtonLeftState;
  }


}

The map() function returns an integer. Having a float is useless.

float percentage = map(val, 0, 1023, 0, 100);

You don't need this line. INPUT_PULLUP does not make sense for an analog.

pinMode (POTPIN, INPUT_PULLUP);

You should not read the button again after you determine it has changed.

if (ButtonLeftState != LastButtonLeft) { // debounce the input button
    delay(50);
    ButtonLeftState = digitalRead(BUTTONLEFT);  // *** remove this line ***
    if (ButtonLeftState == LOW) {
      digitalWrite (YELLOWLED, HIGH);
      if (percentage <= 50) {
        digitalWrite (YELLOWLED, HIGH);
      } else if (percentage >= 50) {
        digitalWrite (YELLOWLED, LOW);
      }
    }

This conditional is ambiguous. You should remove the '=' from one of the conditions:

if (percentage <= 50) {
  digitalWrite (YELLOWLED, HIGH);
} else if (percentage >= 50) {
  digitalWrite (YELLOWLED, LOW);
}

Sadly, it still makes me run into the same problem...

So post your latest code. There's no point us trying to guess what you might have changed.

Steve

It is working just like you programmed it. You are only checking the potentiometer when the button is pressed so the LED will only change when the button is pressed.

Do you want the LED to turn on when the button is pressed? Do you want the LED to turn off when the percentage is greater than 50 or less than 50?

So,
The led needs to turn on when the button is pressed, no matter the state of the potpin.
When the led is on, the potpin should turn it off again when it is greater then 50

Just move the analog check outside of the button check.

const int BUTTONLEFT = 9;
const int YELLOWLED = 7;
const int POTPIN = A0;

int LastButtonLeft = HIGH;
int val = 0;

void setup() {
  pinMode(BUTTONLEFT, INPUT_PULLUP);
  pinMode(POTPIN, INPUT);
  pinMode(YELLOWLED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  int val = analogRead(POTPIN);
  int  percentage = map(val, 0, 1023, 0, 100);
  
  if (percentage >= 50 && digitalRead(YELLOWLED) == HIGH) {
    Serial.println("LED off");
    digitalWrite (YELLOWLED, LOW);
  }

  int ButtonLeftState;
  ButtonLeftState = digitalRead(BUTTONLEFT);
  if (ButtonLeftState != LastButtonLeft) { // debounce the input button
    if (ButtonLeftState == LOW) {
      Serial.println("LED on");
      digitalWrite (YELLOWLED, HIGH);
    }
    LastButtonLeft = ButtonLeftState;
    delay(50);
  }
}