Toggling Buttons

Hi from team 5608! So I’m trying to figure out how to toggle the buttons on our joysticks so that we can turn on and off our solenoids. I keep trying to do it, but they keep conflicting with each other so the pistons either twitch or nothing happens.
So just some background: we have 4 solenoids and 2 joysticks, one joystick for every two solenoids, however Button 3 (raw button) on both joysticks are used to activate their respective solenoids.
Here’s our code (I’ll give you just one of them since the code would be almost identical for the other one):

/**Toggles the right joystick button 3.*/
public void rightToggle(){

if (right.getRawButton(3)){
		switchSol1 = !switchSol1;
	}else {
		isPressedPiston1 = !isPressedPiston1;
	if (switchSol1){
	}else {


In the future, when pasting code wrap the code with code] and /code] tokens so that it will come out as in the quote above rather than your original email. It’s much easier for folks to read code with indentation.

So your code seems to maintain a local boolean isPressedPiston1 for how you last set the solenoid. Then only when you switch from off to on, you set a secondary boolean switchSol1 to actually effect the switch. But you don’t set isPressedPiston1 to true in this case so it stays false, even though you’ve changed the piston state-- unsure if this is what you intended.

I think we need to see the context(s) that you call this rightToggle() function. Since you are checking the state of the button, I would presume you are calling it multiple times from teleopPeriodic() in which case when it is not pressed you will simply be flip-flopping isPressedPiston1. Then when you press the button, the previous state of isPressedPiston1 will be unknown-- fully dependent on the last value it was flip-floped from. It may or may not work. On the other hand if you are only calling this after you know that button is pushed, then there should be no need to check button in this function, or maintain isPressedPiston1.

Please provide the calling context as with just the one function it is difficult to guess what may be wrong.

I’m unsure what you meant this to do, but it looks like you’re trying to implement a 4-state machine with one button. If so, this will not achieve it - once isPressedPiston1 is set to true, it will never toggle again.

In any case, assuming that switchSol1 and isPressedPiston1 are persistent variables after isPressedPiston1 is set true, every time this routine is entered and button 3 is pressed, switchSol1 will toggle. The problem is likely that this routine is called rather quickly, in a loop. If so, this would cause the state of switchSol1 (and therefore sol1 and sol2) to twitch. Depending on your programming model, you can either use a whenPressed() callback to just call this one time when the button is pressed, or include a timeout. That is, when the state is toggled, set a time to wait (perhaps 300 milliseconds to start), and exit this routine immediately if it is called and you have not reached the timeout yet.