Servo Help

Hello,
My team is trying to program a servo so when you press a joystick button it goes to a certain angle. I looked and the wpilib page and I saw the “exampleServo.set(0.5);” example code, but I’m not sure how to use a joystick button with that. I have tried various ways, but nothing has worked. Can anyone help me?
We are using a servo from ServoCity that turns 90°. It’s plugged into a pwm port on the roboRio.

Try adding the following code to your Robot.java file in the robotInit() method (change the value of PWM_SERVO to match the PWM port you used for your servo):


    
    // Set this to the PWM port you plugged your servo into on the roboRIO
    public static final int PWM_SERVO = 8;
    
    // Servo you want to control (give it a name related to its purpose instead of servo)
    private Servo servo;
    
    public void robotInit() {
    	// Construct an instance of your servo (change the name so it is meaningful to its purpose)
    	servo = new Servo(PWM_SERVO);
    	LiveWindow.addActuator("Name of Subsystem", "Servo Name", servo);

        // rest of your robotInit follows ...
    }

Deploy the code and then put your driver station in “Test” mode instead of “Teleop” mode. You should see a control for your servo appear on the smart dash board with a slider. Move the slider around to find the value you want it to go to when the user presses a button.

Next, in your code where you handle the button press, use the servo “set” method with the value determined from test mode to make the servo go to the proper position. Something like the following:


servo.set(VALUE_FROM_TEST_MODE);

Hope that helps.

What is the part number of the servo, and what is it actuating?

The part number is spg5685a-4507a90 and it is changing the position of our shooter