Well, I coded an on/off switch recently and I came up with a one button toggle after a two button toggle.
Code:
public void teleopPeriodic()
{
boolean spikeToggle = false;
spike.set(Relay.Value.kOff);
if(joy1.getRawButton(BUTTON_NUM)) //set the button to activate motor for the joystick
{
if(spikeToggle == false) // if the toggle is off, set to true and activate motor
{
spikeToggle = true;
spike.set(Relay.Value.kOn);
}
Timer.delay(0.5); // cannot change the value within the first half second of activating, you can change value but it is here to have a safe toggle without toggling every millisecond
else if(spikeToggle == true) // if the motor is on, turn it off and it is set to false
{
spikeToggle = false;
spike.set(Relay.Value.kOff);
}
Timer.delay(0.5); // again it stalls the change for convenience and safety
}
}
Two buttons were crazy, but if you want two buttons, just replace the else if with the if statement and add it to the second button loop. Hope this helped.
If you have any questions and comments, just reply or post, etc.
Happy Competitions!