Salutations fellow programmers!
I’m the programmer for team 1989. This is my first year programming so I thought I’d reach out for a little help. Our team is going to have our shooter positioned at a fixed angle (36.8 degrees) and then we are going to vary the speed depending on our distance from the goals. I programmed 3 buttons on the attack 3 joystick for 3 different speeds but the motors for the shooter will only stay on when I am holding the button. I wanted to know if it is possible to program the buttons so that you push it once to turn the motors on and then push it again to turn them off. Nothing I’ve tried has worked so far. Here’s the method I made for the shooter:
public void shooter()
{
final int button1 = 2;
final int button2 = 3;
final int button3 = 4;
double speed = 0;
if (leftstick.getRawButton(button1))//when button 2 is pressed motor moves at 30% of max speed
{
speed = 0.3;
}
if (leftstick.getRawButton(button2))//when button 3 is pressed motor moves at 60% of max speed
{
speed = 0.6;
}
if (leftstick.getRawButton(button3))//when button 4 is pressed motor moves at 90% of max speed
{
speed = 0.9;
}
Victor3.set(speed);
Victor4.set(speed);
}
Real quick. Change the scope of your ‘speed’ variable. It should probably be a class variable. Make sure you provide a button to set the ‘speed’ to zero.
Declare the speed variable only once, so outside the main loop. Also initialize it in robotInit(). Other than that, use a button to set it to 0 or something like that.
It looks like you are only checking the buttons and setting the speed in the constructor, you probably want to put that into a loop. I would move all of the button and speed control stuff into a public method and call it from either a command or teleopPeriodic, depending on if you use command based robot or iterative robot. (If you use simple robot, just call it in a loop)
Thanks so much Ether. I will try it out next time I get to test the robot and let you guys know if I have any other issues. And I already called this method in the teleopPeriodic so that wasn’t an issue.
If you want the other buttons (2 and 3) to be able to change the speed without having to press button1 to stop it first, then make a slight code change as follows: