Coding Motor Using DPad on a controller

I’m still new to coding and i’m wondering how would I code a Dpad to control a motor. I would like the motor to stop when the Dpad is released and the motor to run when the Dpad to pressed. Dpad up goes “Left” and Dpad down controls the motor going down “Right.”

Have you read the section on command based programming? We have found it to be a good framework and the examples show you how to map joystick buttons to commands.

https://docs.wpilib.org/en/latest/docs/software/commandbased/what-is-command-based.html

1 Like

I would reccomend changing the category of this topic to programming

Also, what programming language are you using?

If you’re using the new Command Based framework the easiest way would probably be to create a Button object using a custom Lambda function.

ie.

Button dpadDownButton = new Button(() -> driverController.getPOV() == 180);
dpadDownButton.whenPressed(new MotorCommand(subsystem));

I’m using Java for the code

if using the new command framework in the robot container button binding section add something like this. Just change the subsystems/methods/commands you want to run with it.

new POVButton(driverController, 0)
    .whenPressed(new RunCommand(lift::pov0, lift));

new POVButton(driverController, 90)
    .whenPressed(new RunCommand(lift::pov90, lift));

new POVButton(driverController, 180)
    .whenPressed(new RunCommand(lift::pov180, lift));

new POVButton(driverController, 270)
    .whenPressed(new RunCommand(lift::pov270, lift));

0 degrees is up, 90 is to the right, 180 down, 270 left.

1 Like

I don’t know how I missed that there was a POVButton Class for the Command-Based Rewrite. Yes, use that instead of what I did above.

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