View Single Post
  #6   Spotlight this post!  
Unread 05-02-2013, 18:25
gixxy's Avatar
gixxy gixxy is offline
Programming and Arduino Mentor
AKA: Gustave Michel III
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Nov 2011
Rookie Year: 2012
Location: Ruston, LA
Posts: 207
gixxy is on a distinguished road
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;
    }
  }
}
__________________
Programmer - A creature known for converting Caffeine into Code.
Studying Computer Science @ Louisiana Tech University
Associate Consultant @ Fenway Group

2012-13: 3946 - Head of Programming, Electrical and Web
2014 - 3468 - Programming Mentor
2015 - Present - Lurker
Reply With Quote