While reviewing our shooter code, I figured that the ability to be able to toggle our shooter wheels on/off would be a nice feature to have. However, no matter how hard I tried, I simply could not figure out a way to do this.
The only method that I was able to call upon was just joystickButton.get() since we are using Iterative robot this year.
If it is possible to create an On/Off button, how would it be done?
An On/Off button on the dashboard or on your joystick?
On the joystick. I would prefer a more physical button.
The easiest way would be to save the state as on or off in a boolean or byte variable and then switch it every time the button is pressed.
Pseudo-Code:
if joystick button pressed then
if lightsAreOn is true then
//so some stuff
set lightsAreOn to false
else
//do some stuff
set lightsAreOn to true
That won’t work since teleop loops every 20ms, which means one press will equal a lot of Joystick.get()'s.
Use the Timer to limit how quickly you can toggle.
double interim = 2; //can only toggle every 2 seconds
double previous = 0;
boolean isOn = false; //state of motors
if(JoystickButton.get()) {
/*check if interim time has passed since previous check*/
if(Timer.getFPGATimestamp() - previous >= interim) {
previous == Timer.getFPGATimestamp();
if(isOn == true) {
//change state of motors
isOn = false;
} else {
//change state of motors
isOn = true;
}
}
}
button1 = GetButton(button1);
if (button1 && ! button1previous) toggle1 = !toggle1;
button1previous = button1;
initialize toggle1 to zero.
Thank you. This is exactly what I was looking for but couldn’t write myself
I assume that if you wanted to start with the motors running, I would initialize toggle as true?
Yes. Either that or run the motors when it’s false:)
Not if your using the command-based template.
This is code that I wrote and use. Just make all of your buttons AdvancedButton and call
button#.togglePressed(yourCommand);
some variables are protected so that they cant be accessed but u can still extend the class and make your own addition