|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
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. |
|
#2
|
||||
|
||||
|
Re: Programming Joystick Buttons to toggle a motor on and off
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; } } |
|
#3
|
|||
|
|||
|
Re: Programming Joystick Buttons to toggle a motor on and off
One bug in your code, byteit101
Quote:
|
|
#4
|
||||
|
||||
|
Re: Programming Joystick Buttons to toggle a motor on and off
no, it is in an if else
if it was Code:
if (btn3state)
{
jag3.Set(0.5);
btn3state=false;
}
else if (!btn3state)//always execute
{
jag3.Set(0.0);
btn3state=true;
}
|
|
#5
|
||||
|
||||
|
Re: Programming Joystick Buttons to toggle a motor on and off
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 |
|
#6
|
|||||
|
|||||
|
Re: Programming Joystick Buttons to toggle a motor on and off
Quote:
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
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. |
|
#7
|
|||
|
|||
|
Re: Programming Joystick Buttons to toggle a motor on and off
May I reccomend a simpler system?
Assume the following variables: Jaguar speedcontroller1 Joystick usb3 has been set up and initialized correctly Code:
// 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 ); |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| joystick and extra buttons | TEAM1949 | Programming | 3 | 02-18-2008 01:22 PM |
| Toggle Motor On & Off Help..... | programmr | Programming | 1 | 02-16-2008 11:15 AM |
| Programming Extra Joystick Buttons | Inverted | Programming | 2 | 02-08-2007 11:45 AM |
| Programming motors with joystick buttons | TMHStitans | Programming | 5 | 03-21-2005 09:07 PM |
| joystick buttons and a quick question | h0x4r | Programming | 2 | 02-17-2005 12:18 AM |