View Full Version : Programming Joystick Buttons to toggle a motor on and off
programmr
23-01-2009, 11:29
Hello there, can someone please help? I want to use a joystick button to toggle on and off a motor. If logitech button 3 is pressed once, turn the motor on with a speed of .5 If the button number 3 is pressed again, turn the motor off. In this case, it would be controlled by joystick connected to usb port 3.
Attached is what i have so far. Any help would be greatly appreciated.
byteit101
23-01-2009, 16:50
Joystick stick3(3);//obviously
Jaguar jag1(3);//PWM port 3
bool btn3state=false;
if (stick3.GetRawButton(3))
{
if (btn3state)
{
jag3.Set(0.5);
btn3state=false;
}
else
{
jag3.Set(0.0);
btn3state=true;
}
}
LinuxMercedes
23-01-2009, 20:30
One bug in your code, byteit101
Joystick stick3(3);//obviously
Jaguar jag1(3);//PWM port 3
bool btn3state=false;
if (stick3.GetRawButton(3))
{
if (!btn3state) //if the button has not been depressed
{
jag3.Set(0.5);
btn3state=true; //the button has been depressed
}
else
{
jag3.Set(0.0);
btn3state=false; //the button was clicked again
}
}
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.
byteit101
24-01-2009, 08:05
no, it is in an if else
if it wasif (btn3state)
{
jag3.Set(0.5);
btn3state=false;
}
else if (!btn3state)//always execute
{
jag3.Set(0.0);
btn3state=true;
}
it would be buggy, but if "if" is true, "else" is not executed
This is how my team did it.
// this is all in our autonomous loop....
bool buttonUp = leftStick->GetRawButton(1);
bool buttonDown = leftStick->GetRawButton(2);
bool buttonStop= leftStick->GetRawButton(3);
float elevatorSpeed; // this is the speed of our elevator
if (buttonUP){
elevatorSpeed(1.0);
}
else if (buttonDown){
elevatorSpeed(-1.0);
}
else if (buttonStop){
elevatorSpeed(0.0);
}
ElevatorMotor1->Set(elevatorSpeed); // elevator motors one and two are
ElevatorMotor2->Set(-elevatorSpeed);// on both sides of our elevator
Eric Finn
24-01-2009, 10:01
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.
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.
WatersongX
25-01-2009, 17:47
May I reccomend a simpler system?
Assume the following variables:
Jaguar speedcontroller1
Joystick usb3
has been set up and initialized correctly
// with variable declarations (outside of main loop, so they don't reset every time)
bool togglestate = false;
bool lastu3b3state = false;
// somewhere in code (inside main loop, runs every time)
if ( lastu3b3state == true && usb3.GetRawButton( 3 ) == false ) togglestate = !togglestate;
speedcontroller1.Set( togglestate?0.5f:0.0f );
// at the end of the main loop, or at least after the button is done being used
lastu3b3state = usb3.GetRawButton( 3 );
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.