Button assignment for specific motors

I’ve been having trouble trying to find out how to assign motors to buttons. In vex you could do something like…

if(vexRT[Bn5U] == 1){
motor[left] ==127;
}’(everything probably not exact but its something like this)

so my teammate and I were trying to do the same in java for the frc programming but when we tried it, it did not work out well. We’ve been trying to fix it but were having trouble assigning motors(Victor, Talon, etc.) to buttons on our joystick(all motors, servos, encoders, and counters are initialized) . So if someone can help us out I will really appreciate it because it’s starting to bug me since I couldn’t figure it out. ^W^

This is our code now…

package org.usfirst.frc.team5432.robot;

import edu.wpi.first.wpilibj.CounterBase.EncodingType;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Servo;
import edu.wpi.first.wpilibj.Talon;
import edu.wpi.first.wpilibj.TalonSRX;
import edu.wpi.first.wpilibj.Victor;
import edu.wpi.first.wpilibj.VictorSP;
import edu.wpi.first.wpilibj.buttons.JoystickButton;


  • The VM is configured to automatically run this class, and to call the

  • functions corresponding to each mode, as described in the IterativeRobot

  • documentation. If you change the name of this class or the package after

  • creating this project, you must also update the manifest file in the resource

  • directory.
    public class Robot extends IterativeRobot {
    RobotDrive myRobot;
    Joystick stick;
    Joystick xbox;
    JoystickButton A;
    JoystickButton B;
    JoystickButton X;
    JoystickButton Y;
    JoystickButton LB;
    JoystickButton RB;
    JoystickButton BACK;
    JoystickButton START;

    int counter1 = 0;
    int counter2 = 0;
    int counter3 = 0;
    JoystickButton button1;
    JoystickButton button2;
    JoystickButton button3;
    JoystickButton button4;
    JoystickButton button5;
    JoystickButton button6;
    JoystickButton button7;
    JoystickButton button8;
    JoystickButton button9;
    JoystickButton button10;
    JoystickButton button11;
    JoystickButton button12;
    VictorSP rightMotors;
    VictorSP leftMotors;
    Servo retaining;
    Servo shooting;
    TalonSRX arm;
    Victor shooter;
    Talon lift;
    Talon winch;
    Encoder right;
    Encoder left;
    Encoder shootarm;

    public void roboInit() {
    myRobot = new RobotDrive(0,1);
    xbox = new Joystick(1);
    A = new JoystickButton(xbox,1);
    B = new JoystickButton(xbox,2);
    X = new JoystickButton(xbox,3);
    Y = new JoystickButton(xbox,4);
    LB = new JoystickButton(xbox,5);
    RB = new JoystickButton(xbox,6);

     stick = new Joystick(0);
     button1 = new JoystickButton(stick,1);
     button2 = new JoystickButton(stick,2);
     button3 = new JoystickButton(stick,3);
     button4 = new JoystickButton(stick,4);
     button5 = new JoystickButton(stick,5);
     button6 = new JoystickButton(stick,6);
     button7 = new JoystickButton(stick,7);
     button8 = new JoystickButton(stick,8);
     button9 = new JoystickButton(stick,9);
     button10 = new JoystickButton(stick,10);
     button11 = new JoystickButton(stick,11);
     button12 = new JoystickButton(stick,12);
     retaining = new Servo(9);
     shooting = new Servo(8);
     lift = new Talon(4);
     winch = new Talon(3);
     shooter = new Victor(5);
     leftMotors = new VictorSP(6);
     left = new Encoder(6,counter1,true, EncodingType.k1X);
     rightMotors = new VictorSP(7);
     right = new Encoder(7,counter2,true, EncodingType.k1X);
     arm = new TalonSRX(2);
     shootarm = new Encoder(2,counter3,true, EncodingType.k1X);



    • This function is called periodically during autonomous
      public void autonomousPeriodic() {



    • This function is called periodically during operator control
      public void teleopPeriodic() {



    • This function is called periodically during test mode
      public void testPeriodic() {



The method you need is this:

public boolean getRawButton(final int button);

If you want the arm to move full speed in the positive direction when A on your XBOX controller:

if(A.get()) {

Note that in frc you send motors a value between -1 and 1, with -1 being full reverse, 0 being stopped, and 1 being full forward.

if(A.get()) {
} else {
    arm.set(0); // !!

Motor safety should catch this for you, but please make sure to stop your motors when you’re done with them.

Thank you sooooooooo much Pault, mikets, and euhlmann this will help us write the code. This is just what we need.