Trouble with while and if else stacking

` while(Controller1.ButtonUp.pressing()){

vex::task::sleep(10);

if(Controller1.ButtonUp.pressing()){

vex::task::sleep(10);

armMotor.spin(forward);

vex::task::sleep(10);

}else{armMotor.stop();}}

vex::task::sleep(10);

while(Controller1.ButtonDown.pressing()){

vex::task::sleep(10);

if(Controller1.ButtonDown.pressing()){

vex::task::sleep(10);

armMotor.spin(reverse);

vex::task::sleep(10);

}else{armMotor.stop();}

vex::task::sleep(10);

}

} `

This is my code and I am having trouble with this code the up and down goes indefinitely instead of going to the else because of the while and other times it freezes I tried adding sleeps to make it not skip any lines but it still skips the else

What it is supposed to do is when you press the up arrow it goes up until you let go and the same with the down arrow

Please format your code with triple backticks on either side of the code. Makes it easier to read for everyone else, and to help you. ``` ← these are triple backticks

4 Likes
  1. Please write what this code is meant to do. That way, we can help find problems.
  2. You are basically saying
    While event x is happening,
    if it is actually happening, do output 1 (spin the arm)
    if it is not actually being pressed, stop the arm
    The only way the arm would be stopped would be if you stopped pressing the button in 10 milliseconds, so at the start, write if (Controller1.ButtonUp.pressing()){while(true){[insert code here]}}
    Or, just add a waituntil
    Like



vex::task::sleep(10);

armMotor.spin(forward);

vex::task::sleep(10);
waitUntil(!Controller1.ButtonUp.pressing());
armMotor.stop();
}

if(Controller1.ButtonDown.pressing()){

vex::task::sleep(10);

armMotor.spin(reverse);

vex::task::sleep(10);

waitUntil(!Controller1.ButtonDown.pressing());
armMotor.stop();
} ```
Also, they are not apostrophes, they are back ticks (` not ')
2 Likes

Your while loop exits before the else can ever run, because they are checking for the same thing. If you want it to go on forever you can replace the Controller1.ButtonUp.pressing() in the while with true to make it go on forever. If you then want the loop to stop you can add a break after stoping the motors inside the else statement.

1 Like

The problem is if I put a while true the rest of my code which I am not showcasing will not work because It essentially block it from running

try the new code. also use callbacks or tasks to run multiple things. Example
In driver control, Controller1.ButtonUp.pressed(insertnameoffunction);

2 Likes

Also, make sure you don’t call Controller1.ButtonUp.pressed(insertnameoffunction); multiple times in the loop. Just call it once at the beginning of the program and then function insertnameoffunction will be called (back) by vexos whenever button is pressed, and only once per press, which is nice.

1 Like

I’ve got some better code for you:

while true:
  if ButtonA pressing:
    spin forward
  else if ButtonB pressing:
    spin backward
  else:
    stop spinning

If you don’t understand it, I’ll be happy to explain.

1 Like

Note to OP, this is pseudo code, just in case you don’t see it

1 Like