View Single Post
  #2   Spotlight this post!  
Unread 10-02-2015, 09:19
cstelter cstelter is offline
Programming Mentor
AKA: Craig Stelter
FRC #3018 (Nordic Storm)
Team Role: Mentor
 
Join Date: Apr 2012
Rookie Year: 2012
Location: Mankato, MN
Posts: 77
cstelter will become famous soon enough
Re: Toggling Buttons

Quote:
Originally Posted by nguobadia View Post
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):

Code:
/**Toggles the right joystick button 3.*/
public void rightToggle(){
		
	if (right.getRawButton(3)){
		if(!isPressedPiston1){
			switchSol1 = !switchSol1;
				
		}else {
			isPressedPiston1 = !isPressedPiston1;
				
		}
		if (switchSol1){
			sol1.set(true);
			sol2.set(true);
				
		}else {
			sol1.set(false);
			sol2.set(false);
				
		}
			
	}
}
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.
Reply With Quote