View Single Post
  #4   Spotlight this post!  
Unread 14-01-2014, 14:39
omalleyj omalleyj is offline
Registered User
AKA: Jim O'Malley
FRC #1279 (Cold Fusion)
Team Role: Mentor
 
Join Date: Jan 2008
Rookie Year: 2008
Location: New Jersey
Posts: 132
omalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to behold
Re: TOGGLE code not working

Code:
Button2Value = false;
Toggle = false;

if(stick1.GetRawButton(2))
{
  	if(Button2Value == false)
  	{
  	  	Button2Value = true;
  	  	Toggle = !Toggle;
  	}
}
else
{
  	Button2Value = false;
}
The rest of the code may be important. For instance if this is called in a function the toggle value will be reset every time the function is called. Are you sure the same Toggle is in scope when you check? If either of these is the case put the declaration and initialization of Toggle at a level where is is seen and changed appropriately (class level as public member perhaps).

When button 2 is pressed Toggle will change immediately. When the button is released it will only change the Button2Value on the next call, not right away. Are you sure the calling code allows for that? For instance if you check button 2 is true before calling this code Button2Value would never reset.

There is also a chance that 'bounce' is your problem. Where the button connection is bouncing between true and false at the moment contact is just being made or broken. See http://en.wikipedia.org/wiki/Switch 'contact bounce'. There are a couple of ways to handle that, usually a timer that enforces a minimum time between state changes. A tenth of a second is probably more than sufficient.
Reply With Quote