View Single Post
  #2   Spotlight this post!  
Unread 03-02-2006, 18:06
Dave Scheck's Avatar
Dave Scheck Dave Scheck is offline
Registered User
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Feb 2003
Rookie Year: 2002
Location: Arlington Heights, IL
Posts: 574
Dave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond repute
Re: Beginning programmer: Variable question HELP!!!

Your problem is that you set bob to 1 and then check to see if it is 1 and the button is pressed and set it back to 0.
Code:
if(bob == 2 && p1_sw_trig)
{
  bob = 0;
}
/** assuming bob == 0, bob is set to 1 ****/
if(bob == 0 && p1_sw_trig)
{
  bob = 1;
}
/** bob is now 1 and gets set to 0 ****/
if(bob == 1 && p1_sw_trig)
{
  bob = 0;
}
/*** bob is always 0 if the trigger has been pressed ***/
You can fix this with by using an else if.
Code:
static unsigned int bob = 2;

Void Default_Routine(void)
{
  if(bob == 2 && p1_sw_trig)
  {
    bob = 0;
  }
  else if(bob == 0 && p1_sw_trig)
  {
    bob = 1;
  }
  else if(bob == 1 && p1_sw_trig)
  {
    bob = 0;
  }
}
Once you do that you're going to run into the problem that the state is constantly toggling between 0 and 1 when the button is pressed.

This is because there is no way that you as a user will be able to tap the trigger for 1 loop (1/42 of a second) and the trigger will actually be pressed for multiple loops.

What I think you want to do is keep track of the previous button state and only change states if your current state is pressed and your previous is not.