Changing control scheme mid-match

I have made a code that changes the controls of the robot from a manual if pressing to an automatic spin to a certain point if a button is pressed.
I have been using Booleans to change between them. I can change once, but not back, can you help?
this is an example:

while(sped==false && manual == false){}
Controller1.ButtonRight.pressed(manualOn);
void manualOn(){
manual = true;
}

Is this everything in your driver control? Is there anything out of that while loop?

that is not the driver control that is just how it switches
it changes the Boolean and switches to another while loop with different controls

Could you please share more if your code, in code tags?

sure this is the entire driver control

void usercontrol(void) {
// User control code here, inside the loop
while (1) {
// This is the main execution loop for the user control program.
// Each time through the loop your program should update motor + servo
// values based on feedback from the joysticks.

// ........................................................................
// Insert user code here. This is where you use the joystick values to
// update your motors, etc.
  
// normal drive
// use cubic function
while(sped==false && manual == false){
  // speed is set to joystick values
  LeftA.setVelocity(Controller1.Axis3.value()^3/100,percent);
  LeftB.setVelocity(Controller1.Axis3.value()^3/100,percent);
  RightA.setVelocity(Controller1.Axis2.value()^3/100,percent);
  RightB.setVelocity(Controller1.Axis2.value()^3/100,percent);
  LeftA.spin(forward);
  LeftB.spin(forward);
  RightA.spin(forward);
  RightB.spin(forward);
  // R1 grabs goal with claw
  Controller1.ButtonR2.pressed(grabby1);
  // X lets go of goal with claw
  // stacks goal on platform with Y
  Controller1.ButtonR1.pressed(Stack);
  // A raises goal
  Controller1.ButtonA.pressed(lifty);
  // lift down with B
  Controller1.ButtonB.pressed(lifty2);
  // spins goal lift down with Down
  Controller1.ButtonDown.pressed(Goalliftdown);
  // Left raises goal lift
  Controller1.ButtonLeft.pressed(Goalliftup);
  // spins conveyor
  if(Controller1.ButtonL1.pressing()){
  //spins other direction
    conveyor.spin(forward,80,percent);
  }
   else if(Controller1.ButtonL2.pressing()){
    conveyor.spin(reverse,80,pct);
  }
  else{
  // stops the conveyor from spinning
    conveyor.stop(hold);
    conveyor.setVelocity(0, percent);
  }
  //speed change using an if statement
  Controller1.ButtonY.pressed(spedy);
  // turns on manual
  Controller1.ButtonRight.pressed(manualOn);
}
//speed change and no manual
while(sped==true && manual == false){
  LeftA.setVelocity(Controller1.Axis3.value()*2,percent);
  LeftB.setVelocity(Controller1.Axis3.value()*2,percent);
  RightA.setVelocity(Controller1.Axis2.value()*2,percent);
  RightB.setVelocity(Controller1.Axis2.value()*2,percent);
  LeftA.spin(forward);
  LeftB.spin(forward);
  RightA.spin(forward);
  RightB.spin(forward);
  Controller1.ButtonR2.pressed(grabby1);
  // R2 is used for speed change use up
  // right can be manual
  //Controller1.ButtonR2.pressed(grabby2);
  // lift up 
  Controller1.ButtonA.pressed(lifty);
  // lift down
  Controller1.ButtonB.pressed(lifty2);
  // spins goal lift up
  Controller1.ButtonDown.pressed(Goalliftdown);
  Controller1.ButtonLeft.pressed(Goalliftup);
  if(Controller1.ButtonL2.pressing()){
  //spins other direction
  // find out what problem with reverse is
    conveyor.spin(reverse,80,pct);
  } 
  else if(Controller1.ButtonL1.pressing()){
  // strts coveyor spinning
    conveyor.spin(forward,80,pct);
  }
  else{
  // stops the conveyor from spinning
    conveyor.stop(hold);
    conveyor.setVelocity(0, percent);
  }
  //speed change
  Controller1.ButtonY.pressed(truespedy);
  Controller1.ButtonRight.pressed(manualOn);
}
// speed is normal with Manual
while(sped==false && manual == true){
  LeftA.setVelocity(Controller1.Axis3.value()^3/100,percent);
  LeftB.setVelocity(Controller1.Axis3.value()^3/100,percent);
  RightA.setVelocity(Controller1.Axis2.value()^3/100,percent);
  RightB.setVelocity(Controller1.Axis2.value()^3/100,percent);
  LeftA.spin(forward);
  LeftB.spin(forward);
  RightA.spin(forward);
  RightB.spin(forward);
  if(Controller1.ButtonDown.pressing()){
    Glift.spin(reverse,80,pct);
  }
  // spin the goal lift down after setting velocity.
  else if(Controller1.ButtonLeft.pressing()){
    Glift.spin(forward,80,pct);
  }
  else{
    Glift.setVelocity(0, percent);
    Glift.stop(hold);
  }
  // setting controls for lift
  
  if(Controller1.ButtonA.pressing()){
    // lift goes up
    Lift.spin(reverse,80,pct);
  }
  else if(Controller1.ButtonB.pressing()){
    //lift goes down
    Lift.spin(forward,80,pct);
  }
  else{
    // lift is still.
    Lift.setVelocity(0, percent);
    Lift.stop(hold);
  }
  if(Controller1.ButtonY.pressing()){
    claw.spin(forward,80,pct);
  }
  else if(Controller1.ButtonX.pressing()){
    claw.spin(reverse,80,pct);
  }
  else{
    claw.stop(hold);
    claw.setVelocity(0, percent);
  }
  if(Controller1.ButtonL2.pressing()){
  //spins other direction
  // find out what problem with reverse is
    conveyor.spin(reverse,80,pct);
  } 
  else if(Controller1.ButtonL1.pressing()){
  // strts coveyor spinning
    conveyor.spin(forward,80,pct);
  }
  else{
  // stops the conveyor from spinning
    conveyor.stop(hold);
    conveyor.setVelocity(0, percent);
  }
  //speed change
  Controller1.ButtonR2.pressed(spedy);
  Controller1.ButtonRight.pressed(manualOff);
} 
// speed is fast with manual
while(sped==true && manual == true){
  LeftA.setVelocity(Controller1.Axis3.value()*2,percent);
  LeftB.setVelocity(Controller1.Axis3.value()*2,percent);
  RightA.setVelocity(Controller1.Axis2.value()*2,percent);
  RightB.setVelocity(Controller1.Axis2.value()*2,percent);
  LeftA.spin(forward);
  LeftB.spin(forward);
  RightA.spin(forward);
  RightB.spin(forward);
  if(Controller1.ButtonDown.pressing()){
    Glift.spin(reverse,80,pct);
  }
  // spin the goal lift down after setting velocity.
  else if(Controller1.ButtonLeft.pressing()){
    Glift.spin(forward,80,pct);
  }
  else{
    Glift.setVelocity(0, percent);
    Glift.stop(hold);
  }
  // setting controls for lift
  
  if(Controller1.ButtonA.pressing()){
    // lift goes up
    Lift.spin(reverse,80,pct);
  }
  else if(Controller1.ButtonB.pressing()){
    //lift goes down
    Lift.spin(forward,80,pct);
  }
  else{
    // lift is still.
    Lift.setVelocity(0, percent);
    Lift.stop(hold);
  }
  if(Controller1.ButtonY.pressing()){
    claw.spin(forward,80,pct);
  }
  else if(Controller1.ButtonX.pressing()){
    claw.spin(reverse,80,pct);
  }
  else{
    claw.stop(hold);
    claw.setVelocity(0,pct);
  }
  if(Controller1.ButtonL2.pressing()){
  //spins other direction
  // find out what problem with reverse is
    conveyor.spin(reverse,80,pct);
  } 
  else if(Controller1.ButtonL1.pressing()){
  // strts coveyor spinning
    conveyor.spin(forward,80,pct);
  }
  else{
  // stops the conveyor from spinning
    conveyor.stop(hold);
    conveyor.setVelocity(0, percent);
  }
  //speed change
    Controller1.ButtonR2.pressed(truespedy);

    Controller1.ButtonRight.pressed(manualOff);
}

A fun mathematical way of doing it would be using -1 and 1, something like:

manual = 1;

while (true) {
    If Button Pressed { manual = manual*-1 }

    if (manual == 1)        { Control Scheme 1 }
    else if (manual == -1)  { Control Scheme 2 } 
}
3 Likes

I will try that, but for learning purposes I would like to know why my current method does not work.

It’s because the code runs linearly so once it’s run the while statement once, it doesn’t run it again. Which is why you are able to switch once; because the program is just continuing on with the next while statement.

To make your code do what you want. Try incasing all your while(sped==true && manual) in a global while statement. That is either always true ( while (true) {} ) or have a variable which can end the program such as: while (running == true) {} And switching your whiles to if else statements.

(But I would recommend the -1 and 1, because it’s neater and fun)

3 Likes

Thank You! I will keep the variable method in mind, i am using it for something else, but am going to keep this code the same with a while (true) for now.

1 Like

You have. whole bunch of event registration functions inside a while loop. For example;

Controller1.ButtonRight.pressed(manualOn);

and

Controller1.ButtonR2.pressed(grabby1);

event registration functions should be called once and once only, each time you call pressed it registers another instance of the function.
There have been many topics discussing this, here’s one from earlier today.

3 Likes