Controlling Servo with potentiometer but a very special way

Hi everybody, I'm quite new in the Arduino world and I'm deeply sorry if I do mistakes.

So here's my problem :

I want to control the angle position of a Servo motor using a potentiometer, when it is > or < at some point, I want the servo motor to run continuously in the good direction. So the potentiometer doesn't controle exactly the servo motor but a variable which increases or decreases.

For that I tried to make a first code only for the control of this variable by the potentiometer.

Also I took care of having a dead zone (between 545 and 490) for the potentiometer so when I turn back into the dead zone, it stops the variable frome increasing or decreasing.

The increase phase works, when I turn back into the Dead Zone the variable stops but if I try to decrease or increase it again, it doesn't want to continue.

What did I do wrong ?
Should I do it an other way ?

here is the code :

int val = 0; // Variable which will control the servo motor

int pot = 0; // potentiometer

void setup()
{
  pinMode(A0, INPUT);
  Serial.begin(9600);

}

void loop()
{
  pot = analogRead(A0);
  if (pot > 545) {
    if (val > 0) {
      while (!(val <= 0)) {
        if (pot > 545) {
          val = (val - 1);
          Serial.println(val);//This is to follow the count on Tinkercad serial monitor
          delay(100); // Wait for 100 millisecond(s)
          pot = analogRead(A0);
        }
      }
    }
  }
  pot = analogRead(A0);
  if (pot < 490) {
    if (val < 180) { //Max degree for Servomotor
      while (!(val >= 180)) { 
        if (pot < 490) {
          val = (val + 1);
          Serial.println(val);
          delay(100); // Wait for 100 millisecond(s)
          pot = analogRead(A0);
        }
      }
    }
  }
}

Sorry for my english, i'm not a native speaker and thank you for reading this

The while loops are killing you. Let the loop do the looping. Don't trap your code.

 while (!(val <= 0)) {
        if (pot > 545) {
          val = (val - 1);
          Serial.println(val);//This is to follow the count on Tinkercad serial monitor
          delay(100); // Wait for 100 millisecond(s)
          pot = analogRead(A0);
        }
      }

Let's say that val is greater than 0 so you go into this while loop. Now you turn the pot back to the dead zone so you quit decreasing val. How will val ever get below 0 to end the while loop? It won't. So you're stuck there forever.

Thank you Delta_G for this fast answer.

If I understand correctly, I should remove the "While" loop and only let the "if" do their jobs ?

zerhorace:
Thank you Delta_G for this fast answer.

If I understand correctly, I should remove the "While" loop and only let the "if" do their jobs ?

Yes. You'll likely need another if statement to make sure that you don't increment or decrement too far though.

Delta G, I have no words to tell you how much I thank you !

Now it works fine !

Thank you very much !