While loop not looping?

Using the stock Clawbot Competition Template, and adding only the following 4 lines in User Control mode, the drivetrain doesn’t stop when the button is no longer pressed. It didn’t used to behave this way. In drive mode the button release is recognized, so its not a button or communication issue. These lines come after the usual void user control(void){ statement and are within the while (1){ loop, which is properly closed. Any thoughts?

while (Controller1.ButtonUp.pressing()){
Drivetrain.drive(forward);
wait(20, msec);
}

Unless you tell the drive to stop somewhere it will continue using the last command you sent to it.

2 Likes

In addition you probably don’t want to use a while loop to check if the button is being pressed - otherwise you will be unable to respond to other controller input while the button is being held down. The standard practice is to instead use an if statement inside the infinite loop, like this:

if (Controller1.ButtonUp.pressing()){
    Drivetrain.drive(forward);
}
else{
    Drivetrain.stop();
}
2 Likes

Thanks, much appreciated advice

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