Need a Code for Solenoid to Shift

Hi, I’m a new programmer. I’ve been assigned the task to make a solenoid shift when a button is pushed (such as buttons 4 and 5 on a joystick [tank drive]).
If anyone has a fully functional code that allows a solenoid to shift with a push of a button, please reply with it here. It would be very helpful and I’d really appreciate it.
Thank you! :slight_smile:

What language?


Language as in, does your team use C++, Java, LabVIEW?

Sorry, what programming language? Java, C++, or LabView.

What happened to the last person to answer that question that way:

And it includes a Labview example.

Sorry, haha. It’s Java.

Thanks for clarifying. I didn’t know, haha.

Assuming you have a double-acting solenoid, you want to use the DoubleSolenoid class. DoubleSolenoid.set() is the method that shifts the solenoid.

The code below allows a joystick on port 1, to extend/retract a solenoid on port 1 by pushing button 1. It also adds a simple debump statement which makes the solenoid change state only when the button is first press.


//Instance Variables
public Solenoid shifter;
public Joystick joy;

shifter = new Solenoid(1);//1 is the port number
joy = new Joystick(1);

boolean firstPress = true;
boolean inHighGear = false;

if(joy.getRawButton(1) && firstPress){//first button
inHighGear = !inHighGear;
firstPress = false;
}else if(!joy.getRawButton(1)){
firstPress = true;


Hope this helps!!