View Single Post
  #6   Spotlight this post!  
Unread 24-01-2009, 10:01
Eric Finn's Avatar
Eric Finn Eric Finn is offline
Registered User
FRC #0166 (Chop Shop)
Team Role: College Student
 
Join Date: May 2006
Rookie Year: 2005
Location: Merrimack, NH
Posts: 101
Eric Finn has a spectacular aura aboutEric Finn has a spectacular aura about
Send a message via AIM to Eric Finn
Re: Programming Joystick Buttons to toggle a motor on and off

Quote:
Originally Posted by LinuxMercedes View Post
One bug in your code, byteit101
...
Otherwise, you'd click the button first to set the jag to 0, then the second click would set it to 0.5. Unless I missed something.
Even with that, it would not work properly, assuming this code is executed often enough.
Code:
Joystick stick3(3);//obviously
Jaguar jag1(3);//PWM port 3
bool btn3state=0;
bool pwm3state=0

//The motor starts at 0, each time we press the button it toggles between 0 and .5.
//The motor will only change once if the button is held down for multiple loops.
if(stick3.GetRawButton(3) && !btn3state) //if the button is pressed, but not held...
{
	btn3state = 1; //button is now being held
	if(!pwm3state) //if the motor is not moving
	{
		jag3.Set(0.5);
		pwm3state=1; //the motor is moving
	}
	else //if the motor is moving
		{
		jag3.Set(0.0);
		pwm3state=0; //the motor is not moving
	}
}
else if(!stick3.GetRawButton(3) && btn3state) //if we think the button is held, but it's not being pressed...
	btn3state = 0; //button is no longer held
With this, you have to release the joystick button in order to toggle the motor again. Without this, the motor will toggle many times, even with just a short tap of the button.
Remember, if that code is in a while loop, the variables should be declared *before* the loop. If the code is in a function, they should be static.
__________________
It always takes longer than you expect, even when you take into account Hofstadter's Law.
--Hofstadter's Law

Reply With Quote