View unanswered posts | View active topics It is currently Thu Oct 30, 2014 9:51 am






Reply to topic  [ 2 posts ] 
Button Control 
Author Message
Rookie

Joined: Mon Dec 14, 2009 8:29 pm
Posts: 6
Post Button Control
Hi. In my program, I wan't to be able to press a button once to turn on a motor and then press the same button again to stop the motor.

I can make the motor run as long as a button is held (if(joyBtn(==1)) but that really won't work with the rest of our set up. Im at a loss at how to do this. I'm thinking i need some sort of counter but i have no idea how to set something like that up. Any help as to how I might go about this would be great.


Thu Feb 18, 2010 2:53 am
Profile
Expert

Joined: Mon Oct 27, 2008 9:59 pm
Posts: 139
Post Re: Button Control
As always, there a many approaches you could take to achieve this.

One of the simplest ones to me is to use a control flag that you flip on/off each time you press the joystick button.
For example:
Code:
task main()
{
   bool motorOn = false;  //motor control flag

   while(true)
   {

      if(joy1Btn(x)) //if button is pressed
      {
         motorOn=!motorOn;  //flip the flag (if true, set to false; if false, set to true)
      }

      //actual motor control
      if(motorOn) //check control flag and turn the motor on or off
      {
         motor(myMotor)=100; //on
      }
      else
      {
         motor(myMotor)=0; //off
      }
   }
}

Of course there is a huge gotcha in the example above. What happens if you hold the button down for more than a few milliseconds (which is more than likely)? The while loop would flip the flag on every iteration which means that the flag could get flipped many many times each time you press the button because of how much faster the microcontroller is than your thumb.
We can introduce another flag to track whether or not we just flipped the flag to avoid the issue.
Here is a better example showing the second flag:
Code:
task main()
{
   bool motorOn = false;  //motor control flag
   bool justFlipped = false; //mutex flag to avoid flipping control flag when button held down

   while(true)
   {

      if(joy1Btn(x) && !justFlipped) //if button is pressed & flag not just flipped
      {
         motorOn=!motorOn;  //flip the flag (if true, set to false; if false, set to true)
         justFlipped = true;  //set the justFlipped flag to avoid ocillating the motor control flag
      }
      else if(!joy1Btn(x) && justFlipped) //if the button is released
      {
         justFlipped = false;  //reset the justFlipped flag
      }

      //actual motor control
      if(motorOn) //check control flag and turn the motor on or off
      {
         motor(myMotor)=100; //on
      }
      else
      {
         motor(myMotor)=0; //off
      }

   }
}

The code above is just an example, so feel free to experiment what works best for you.


Thu Feb 18, 2010 10:54 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.