Arms won't stop lifting (Programming error)

So we are trying to make a manual function that will lift our arms when we press the L1 and go down when we press L2. When we press the L1 button once, the arms go up infinitely until we press the button again or turn off the program. We want our arms to stop lifting when we let go of L1 or L2. Here is our code:

if (MasterController.ButtonL2.pressing()) {
          LeftBarMotor.spin(directionType::fwd, 50 , percentUnits::pct);
          RightBarMotor.spin(directionType::fwd, 50 , percentUnits::pct);
          StackRelease.rotateTo(CurrentStackerliftpos, rotationUnits::deg);
      } else if(MasterController.ButtonL1.pressing()) {
   
          LeftBarMotor.spin(directionType::rev, 50 , percentUnits::pct);
          RightBarMotor.spin(directionType::rev, 50 , percentUnits::pct);
          StackRelease.rotateTo(CurrentStackerliftpos, rotationUnits::deg);
      } else {
          LeftBarMotor.stop(brakeType::hold);
          RightBarMotor.stop(brakeType::hold);
      }

We really can’t figure out what is wrong with our code. Any idea what is wrong?

You want to change the braketype to either coast or brake. Coast means the motor wont run, brake means it will run to keep its position.

Actually hold makes it keep its position, brake makes it exert force to stay in place but will not return to its original position. Also, I see nothing wrong with this code. Would anything else in the program be making the arms move?

2 Likes

Not that I can see. The only other place we use the arms is a function that lifts the arms to tower height and to block collecting position, but they are separate functions.

Try removing the rotateTo part and see if it works just to test

3 Likes

Try commenting out the other functions that move these motors. This will let you isolate the cause. If this code works as expected, then comment this code out and see if the other functions work.

If both work individually, but do not work when both are included then you have some additional work to do to make sure the blended functionality works as expected.

You also might write a print statement to the Controller Screen that would help you identify which if statements are causing the motors to move

1 Like

The third argument for rotateTo() function waitForCompletion has a default value of true if you don’t specify it explicitly:

vex::motor::rotateTo(double rotation, rotationUnits units, bool waitForCompletion = true)

That means that if StackRelease motor cannot reach its target position, then rotateTo() function will not return control back to your main program and bar motors will continue spinning.

The easiest fix is to pass false as the third argument:

if (MasterController.ButtonL2.pressing())
{
    LeftBarMotor.spin(directionType::fwd, 50 , percentUnits::pct);
    RightBarMotor.spin(directionType::fwd, 50 , percentUnits::pct); 
    StackRelease.rotateTo(CurrentStackerliftpos, rotationUnits::deg, false);
}
else if(MasterController.ButtonL1.pressing())
{
    LeftBarMotor.spin(directionType::rev, 50 , percentUnits::pct); 
    RightBarMotor.spin(directionType::rev, 50 , percentUnits::pct); 
    StackRelease.rotateTo(CurrentStackerliftpos, rotationUnits::deg, false);
}
else
{
    LeftBarMotor.stop(brakeType::hold);
    RightBarMotor.stop(brakeType::hold);
}
1 Like

Ok Thanks @technik3k your solution worked.