Potentiometer code not working

Hello, we are trying to use a potentiometer for our mobile goal, but we are encountering some problems with our code. Every time we press the button, the mobile goal repeatedly goes up and down.
This is our code:
-pot is the name for potentiometer
-1400 is the value we want the mobile goal to stop at
-motor[FrontF] is the name for the mobile goal motor
-mogoStack is the boolean that detects whether button 7U is pressed
-We included the section for reversing the mobile goal to counter momentum

if (vexRT[Btn7U] == 1) {
if (SensorValue(pot) != 1400) {
mogoStack = true;
}
}
while (mogoStack == true) {
while (SensorValue[pot] > 1400) {
motor[FrontF] = -80;
if (SensorValue[pot] <= 1400) {
motor[FrontF] = 20;
sleep(5);
motor[FrontF] = 0;
mogoStack = false;
break;
}
}
while (SensorValue(pot) < 1400) {
motor[FrontF] = 90;
if (SensorValue(opt) >= 1400) {
motor[FrontF] = -20;
sleep(4);
motor[FrontF] = 0;
mogoStack = false;
break;
}
}
}

What do you guys think is causing this problem?

I haven’t looked at your code in any detail, but be aware that it’s extremely unlikely that the robot is going to ever land on exactly a pot value of 1400. You should probably give it an acceptable range, like between 1380 and 1420 or something like that.

Where should we have a range, and how would it help?

The momentum of the mechanism, among other factors, will make it extremely improbable that your robot will stop at a potentiometer value of exactly 1400.

Your code looks to be unconventional in its logic, and, as such, I currently do not have the time to tell you exactly how to implement the range though. Perhaps someone else can help you there.

Im not a really good programmer but i think this might be the problem:

while (SensorValue(pot) < 1400) {
motor[FrontF] = 90;
if (SensorValue(optpot) >= 1400) {
motor[FrontF] = -20;

Hope this helps

EDIT: to fix grammer

To provide a range of acceptable pot values, you might try to use Boolean logic operators, such as the “and” operator, which in RobotC is “”&&".

See: http://cdn.robotc.net/pdfs/natural-language/hp_boolean_logic.pdf

In pseudocode, it might look something like:

while (SensorValue[pot] < 1360)
{
//Arm is too low
move the motor up
}

while (SensorValue[pot] > 1470)
{
//Arm is too high
move the motor down
}

If ( (SensorValue[pot] >= 1360) && (SensorValue[pot] <= 1470) )
//The arm is inside the goldilocks range
Turn the motor off

As has already been mentioned, if your machine has too much inertia when it tries to enter the acceptable range, then it will overshoot and go out of range even though the motor power has been removed. You might have to lower your motor values so you don’t overshoot, or you might need to widen your acceptable range.

There are other methods using PID, but I’m guessing that is beyond your abilities right now.

Even disregarding inertia, the potentiometer will rarely go to an exact measure even if moved precisely by hand. Instead of saying “if the potentiometer value is 1400,” say “if the potentiometer measure is greater than 1380 and less than 1420,” essentially.