Command-based button binding question

This is my first Chielf Delphi post so thank you for your patience!

I want to run another command while I press button 3 on my controller. I would like to toggle between running and not running the command, so I found .whiletrue on the WPILIB.

    // While holding the shoulder button, drive at half speed
    new JoystickButton(m_driverController, Button.kRightBumper.value)
        .whileTrue(new HalveDriveSpeed(m_robotDrive));

I used this to create:

new JoystickButton(driverJoytick, 1).whileTrue(new otherCommand(swerveSubsystem));

However, the .whiletTrue gives an error and says

The method whileTrue(otherCommand) is undefined for the type JoystickButtonJava(67108964)

I’m confused what “Button.kRightBumper.value” could refer to. On the page, it used

Trigger yButton = new JoystickButton(exampleController, XboxController.Button.kY.value);

How do I use just the regular port numbers?

If i were to guess, I’d say your “otherCommand” is not properly extending Command. But I’d need to see more code to say for sure

I agree with the above. Probably an issue with that you are passing as your command.

You are doing that correctly already. If you look at the Documentation for the JoystickButton constructor it takes a GenericHID object (a joystick object) and an int for the button number.

The Button.kRightBumper.value and XboxController.Button.kY.value are just integers wrapped in other classes and variables so you can reference the name of the button for common controllers rather than having to look up the numbers for the “X” or “Y” Buttons.

For some reason when I did

new JoystickButton(driverJoytick, 3).whileActiveOnce(new otherCommand(swerveSubsystem));

It worked fine so maybe that is the correct thing?

You are using the 2023 WPILib, correct? whileActiveOnce is the old method, and while it still exists in the 2023 WPILib, it is deprecated and replaced by whileTrue. So if you’re using the 2022 WPIlib still, you won’t have whileTrue.

1 Like

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