View Single Post
  #3   Spotlight this post!  
Unread 13-02-2010, 14:12
daltore's Avatar
daltore daltore is offline
Electronics/programming/design
AKA: Aaron Osmer
FRC #3529 (ausTIN CANs)
Team Role: Mentor
 
Join Date: Dec 2007
Rookie Year: 2007
Location: San Antonio, TX
Posts: 272
daltore has a spectacular aura aboutdaltore has a spectacular aura aboutdaltore has a spectacular aura about
Send a message via AIM to daltore Send a message via MSN to daltore Send a message via Yahoo to daltore
Re: Toggle through button?

I don't know how the Java code handles the button calls, but wouldn't that just cycle through the numbers without stopping? Unless getButton removes the read value when you read it the first time, this would set one number one loop, and see that the button is still pressed in the next loop and set the next number. You need a second variable to hold the last read button value. In the same Java code, it would look like this:

Code:
if joystick.getButton(2)==true && number<2 && lastState==false {
     number++
} else if joystick.getButton(2)==true && number==2 && lastState==false {
     number = 0
}
   lastState=joystick.getButton(2);
Since lastState is set after the if() structure, the new value will not be read until the next run through. So if it's FALSE now and you push the button, it will increment the variable, then set lastState to TRUE, which will be read the next time, and the structure will not run. Then, when you release the button, it sets lastState to FALSE again, and you can increment the variable again.

You can also use a shorter code system utilizing modulus. Modulus (%) reports the remainder in a division. So, 5 % 3 = 2. This will only report the values 0 (divisible by 3), 1 (1 more than a factor of 3) and 2 (2 more than a factor of 3). This will allow you to use a slightly longer equation, but shorter code which might actually run a bit faster (however insignificant with a 400Mhz processor):

Code:
if joystick.getButton(2) == true && lastState == false {
     number = (number+1) % 3;
}
   lastState = joystick.getButton(2);
Good luck, and let us know if you have any more problems.