Creating a Command that Functions like an on/off switch

Hi everyone, I hope your build seasons are all going well.

This year is my first year programming independently from other programmers, and I just had a quick question.

I apologize if I am ignorant of this not being possible, or a simple way to do this. I was curious of the opinions of those on here before I did detailed research into documentation.

Our Robot is written in Java and based on the CommandBased template provided by FIRST.

I’m aware of it being possible to run a command while a button is held on a Joystick, but is it possible to make sort of an on and off switch?
Example: Press one to activate, press once to deactivate

We are interested in our shooter and intake being a toggle switch instead of a held button.

Thank you in advance

Take a look at toggleWhenActive/toggleWhenPressed - haven’t used it before, but sounds like what you’re looking for.

One of the major downsides to this approach is that it requires the drivers to understand the current state of the robot.

We have done this in years past where we had a 2 gear shifting gear box and wanted to force being in high gear, or maybe toggling on/off LEDs on a camera or something.

Here’s an example of a command that toggles the intake from the 2020 robot of ours. Pushing it once drops it down, pushing it again pulls it up.

Notice that it requires the command to query the state of the subsytem and react accordingly. With something like this, it was very obvious for the driver to know whether the state was up or down because you can see it so that downside of the toggle commands wasn’t quite as large here.


Agreed, and the docs state the same (with good reason).

We took a similar approach this year to toggle soft limits, but we intend to use it only in the pit to reset our climber arm. In our case, we use an inline command and show the state on the dashboard for good measure.

Thank you all for the quick responses. I am going to attempt this once our builders have the shooter operating, and then I will mark a solution.

We are all okay with having to know the state of the robot, though I did bring that up to the rest of the team.

In our case, we use an inline command and show the state on the dashboard for good measure.

I also plan to implement this to our robot.

1 Like

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