Help making making a toggle button

Hi!

So, since our lead programmer left us I’ve been trying to make the code of the robot. Yesterday i was trying to make our solenoid to open and close when pressing the same button on our xbox controller… but i have no idea on how to achive this. We’re using the timedrobot template on Java.

Thanks ins advance!

You’re looking for the toggleWhenPressed function when creating the button.

Button = new JoystickButton(Joystick, 1);

Button.toggleWhenPressed(new Command());

1 Like

Do i need to have the IO class in the code?

The OI definitely helps keeps things organized. It’s created when you start a new project in VSCode.

So, sinice I’m on the timedrobot template could I just create a new file under the src/java and the just name it IO. Or do I need to be on the commmandrobot one to make it work?

Thanks for your help!

To use the Button objects effectively for this purpose, you’d want to be using the CommandBased Robot Template. Which personally, if you lost your senior level developers, I would recommend. It may seem more complex on the surface, but it is great for keeping things organized. There are plenty of videos, and of course the guides on the WPILIB ScreenStepsLive site for using the CommandBased Template.

If you’d rather stick with the TimedRobot, what you’d want to do is use the Joystick Object’s getRawButtonPressed(int button) method to check if the button has been pressed (after being released), then check the current state of the solenoid and invert it.

if(joystick.getRawButtonPressed(buttonNum)) {
   solenoid.set(!solenoid.get())
}

You can see the description of the getRawButtonPressed(int) method here: http://first.wpi.edu/FRC/roborio/release/docs/java/edu/wpi/first/wpilibj/GenericHID.html#getRawButtonPressed(int)

1 Like

Thanks!

Also you can use a boolean.

Do what gixxy said but add a debouncer because if you press the button it will register multiple times causing the solenoid to stutter a bit.

https://frc-pdr.readthedocs.io/en/latest/user_input/joystick.html#debouncers

1 Like

I was under the impression debouncing was built in. If it is not, absolutely, add debouncing.

Turns out if you want to use the better built in Joystick Button Command bindings without using the full Command-Based template and system you can.

You can, but the method outlined in that post won’t work well for button.toggleWhenPressed(). The “toggle” functionality relies on the command persisting after it is initially started, so that it can then be cancelled when the button is pressed again. This won’t work with an InstantCommand, and thus cannot be simply implemented in a way comparable to a lambda or a method reference. One could still accomplish it with an anonymous inner class, but at that point it’s verbose enough that you’re probably better off just writing a command normally. Alternatively, the best option is probably to have the toggling logic in the method itself, rather than in the button binding, and to simply bind it with button.whenPressed().

I will opine, however, that toggles are generally a bad idea as they require the robot operator to mentally keep track of robot state. Unless you are running out of buttons, it is almost always a better idea to avoid a toggle and use two separate buttons.

Glad to have your input on this then!

@Dany_Mont Since you are new to it, check out this great presentation that Triple Helix has put together.

It really simplifies how Command Based Robots work:
http://team2363.org/2016/11/command-based-java-for-frc/

2 Likes