Problems with whileHeld method

Hi all,
I’m having trouble programming 2 motors to run while I am holding down a button. I have set the buttons in the OI as whileHeld, but when I start my program on the robot to test it, it continuously runs, even when I let go of the button. Disabling the robot in the driver station stops it, but when I re-enable it, without even pressing anything on my controller, the motors start to run, like wise before I disabled it. If there’s anybody that could provide assistance that would be highly appreciated.

Do you ever tell the motors not to run when the button isn’t being held? Otherwise they never receive a command other than the one sent while held, so they just keep running with the last instruction.

Building on what Chris is me said. When you let go of a button tied to a “whileHeld” Trigger, the command is cancelled, meaning the interrupted() method of the command will be called. You need to use that method to stop the motors. Otherwise the motors will continue to follow the last command given, which in this case is likely the last speed setting given in your execute() method.

If that doesn’t resolve your issue, please post your code, as it makes it much easier to help you debug issues. Thanks.

1 Like

I understand. I was under the impression that when I let go of the button, the end() method would be called. Thank you both for your help.

if you already have your motors stopping in the end() method, then you can just call end() from interrupted()

To be safe, when removing commenting and setting up a command how I like, I always call end() from interrupted() even if I don’t use the end() for anything so that I can depend on it later.