Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   How to create an On/Off button? (http://www.chiefdelphi.com/forums/showthread.php?t=112759)

thecakeisalie 04-02-2013 20:11

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?

Team3266Spencer 04-02-2013 22:24

Re: How to create an On/Off button?
 
An On/Off button on the dashboard or on your joystick?

thecakeisalie 04-02-2013 22:43

Re: How to create an On/Off button?
 
On the joystick. I would prefer a more physical button. :)

Team3266Spencer 04-02-2013 22:55

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


thecakeisalie 05-02-2013 17:48

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.

gixxy 05-02-2013 18:25

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;
    }
  }
}


Ether 05-02-2013 18:38

Re: How to create an On/Off button?
 

Code:

button1 = GetButton(button1);
if (button1 && ! button1previous) toggle1 = !toggle1;
button1previous = button1;

initialize toggle1 to zero.



thecakeisalie 05-02-2013 20:03

Re: How to create an On/Off button?
 
Quote:

Originally Posted by Ether (Post 1228319)

Code:

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

Re: How to create an On/Off button?
 
Quote:

Originally Posted by thecakeisalie (Post 1228376)
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

Re: How to create an On/Off button?
 
Quote:

Originally Posted by thecakeisalie (Post 1228285)
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

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


All times are GMT -5. The time now is 09:52.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi