Button programming

Can someone explain to me how we can group two motors together and get them run together with a button.It would be a big help it’s been a bit difficult for me to do so any help would be amazing because the docs haven’t really helped me

We are using can bus for motors and Java for code

What motor controllers are you using? And command based or timed?

Command and spark max

Okay, are these in the same subsystem or different subsystems?

If they are in the same subsystem and you want them to move exactly the same, use follow() to make one follow the other.

If they are in separate subsystems, you can bind both commands to the same Trigger in configureButtonBindings() or you can declare a SequentialCommandGroup.

That’s what I’m trying to use is the follow but how would I make that used through a button

In RobotContainer

The follow will be in your subsystem and you will have a method that runs the motors. The button is separate and is in RobotContainer where you will declare something like:
controller.button().whileTrue( new RunCommand(() -> subsystem.method(), subsystem));

1 Like

Do you have a example of this because its still not making complete sense to me l

Subsystem:


import com.revrobotics.CANSparkMax;
import com.revrobotics.CANSparkLowLevel.MotorType;

import edu.wpi.first.wpilibj2.command.SubsystemBase;

public class Subsystem extends SubsystemBase {
  private final CANSparkMax one;
  private final CANSparkMax two;
  /** Creates a new subsystem. */
  public Subsystem() {
    one = new CANSparkMax(1, MotorType.kBrushless);
    two = new CANSparkMax(2, MotorType.kBrushless);
    two.follow(one);
  }

  public void run() {
    one.set(0.2);
  }
}

RobotContainer:


import frc.robot.Constants.OperatorConstants;
import frc.robot.subsystems.Subsystem;
import edu.wpi.first.wpilibj2.command.RunCommand;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;

public class RobotContainer {
  // The robot's subsystems and commands are defined here...
  private final Subsystem m_subsystem = new Subsystem();

  // Replace with CommandPS4Controller or CommandJoystick if needed
  private final CommandXboxController m_driverController =
      new CommandXboxController(OperatorConstants.kDriverControllerPort);

  /** The container for the robot. Contains subsystems, OI devices, and commands. */
  public RobotContainer() {
    // Configure the trigger bindings
    configureBindings();
  }

  private void configureBindings() {
    m_driverController.x().onTrue(
      m_subsystem.runOnce(m_subsystem.run()));
  }
}

You should use subsystem.runOnce(…) instead of new InstantCommand(…, subsystem). Also, a whileTrue binding of an InstantCommand is equivalent to (the simpler, better-named in this case) onTrue.

1 Like

What’s the reason for why subsystem.runOnce(); is better than an instant command?

We are trying to use the code provided but when we get to the run at the very end in robot container it’s telling us that it dosent exist in robot container. If you could help us fix this it would be amazing

I believe that was a typo, and the line should be

m_subsystem.runOnce(m_subsystem.run()));
1 Like


This is what it’s telling me now

Sorry about that, bad method name on my part. run() is a method to run a runnable action and that is what is happening here. Replace that with whatever your method is called.

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