Log in

View Full Version : How to create an On/Off button?


thecakeisalie
04-02-2013, 20:11
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?

Team3266Spencer
04-02-2013, 22:24
An On/Off button on the dashboard or on your joystick?

thecakeisalie
04-02-2013, 22:43
On the joystick. I would prefer a more physical button. :)

Team3266Spencer
04-02-2013, 22:55
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

thecakeisalie
05-02-2013, 17:48
That won't work since teleop loops every 20ms, which means one press will equal a lot of Joystick.get()'s.

gixxy
05-02-2013, 18:25
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;
}
}
}

Ether
05-02-2013, 18:38
button1 = GetButton(button1);
if (button1 && ! button1previous) toggle1 = !toggle1;
button1previous = button1;

initialize toggle1 to zero.

thecakeisalie
05-02-2013, 20:03
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 :P
I assume that if you wanted to start with the motors running, I would initialize toggle as true?

Ether
05-02-2013, 20:05
Thank you. This is exactly what I was looking for but couldn't write myself :P
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:)

Team3266Spencer
05-02-2013, 20:50
That won't work since teleop loops every 20ms, which means one press will equal a lot of Joystick.get()'s.

Not if your using the command-based template.

Arhowk
07-02-2013, 15:50
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

code (http://pastebin.com/VpuPVdcF)