|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
How to create an On/Off button?
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? |
|
#2
|
||||
|
||||
|
Re: How to create an On/Off button?
An On/Off button on the dashboard or on your joystick?
|
|
#3
|
||||
|
||||
|
Re: How to create an On/Off button?
On the joystick. I would prefer a more physical button.
![]() |
|
#4
|
||||
|
||||
|
Re: How to create an On/Off 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: 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
|
|
#5
|
||||
|
||||
|
Re: How to create an On/Off button?
That won't work since teleop loops every 20ms, which means one press will equal a lot of Joystick.get()'s.
|
|
#6
|
||||
|
||||
|
Re: How to create an On/Off button?
Use the Timer to limit how quickly you can toggle.
Code:
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;
}
}
}
|
|
#7
|
||||
|
||||
|
Re: How to create an On/Off button?
Code:
button1 = GetButton(button1); if (button1 && ! button1previous) toggle1 = !toggle1; button1previous = button1; |
|
#8
|
||||
|
||||
|
Re: How to create an On/Off button?
Quote:
![]() I assume that if you wanted to start with the motors running, I would initialize toggle as true? |
|
#9
|
||||
|
||||
|
Re: How to create an On/Off button?
Quote:
|
|
#10
|
||||
|
||||
|
Re: How to create an On/Off button?
Not if your using the command-based template.
|
|
#11
|
||||
|
||||
|
Re: How to create an On/Off button?
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 |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|