Quote:
Originally Posted by LinuxMercedes
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.