How To code a piston using one button

So we have our pneumatic pistons working with two buttons, but we want to only use one button. What is the command in Java to retract the pistons after pressing the same button again?

This is commonly called a toggle. In general if possible teams should avoid using a toggle as it requires the operator to remember which state the mechanism is in. This concern is diminished if it is easily visible from any angle what state the robot is in.

The way you can do this inlined is by using a conditional command that gets the state of the cylinder and configure the on true / false commands to change to the opposite position.

If you are using the DoubleSolenoid class, there exists a function DoubleSolenoid.toggle() that will switch the solenoids position. See here.

Forgot about that. The same thing is there for Solenoid too!

1 Like

You could also create a JoystickButton for this. For example,

JoystickButton pistonToggle = new JoystickButton (<joystick name>, <button name>);
pistonToggle.toggleWhenPressed(<command name>);

For the command:

public void execute () {
    solenoid.set(true);
}

public void end (boolean interrupted) {
    solenoid.set(false);
}

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