I noticed that you never initialize the running variable in your code, that might cause some problems. Commands have a built-in timeout mechanism for doing tasks like the one you're trying to do. So you can run a motor for half speed for 3 seconds like this:
Code:
public class Drive1 extends Command {
public Drive1() {
requires(Robot.driveTrain);
setTimeout(3); // 3 second timeout for the command
}
protected void initialize() {
Robot.driveTrain.runMotor(0.5); // start the motor running 1/2 speed
}
protected void execute() { // motor is already running, so nothing here
}
protected boolean isFinished() {
return isTimedOut(); // wait for 3 seconds to run out
}
protected void end() {
Robot.driveTrain.runMotor1(0.0); // stop the motor on the timeout
}
protected void interrupted() {
end(); // this is here in case the command is interrupted
}
}
All the RobotBuilder comments are removed for clarity - keep them in if you intend to keep using RobotBuilder.
This code:
Code:
joystickButton2 = new JoystickButton(joystick1, 2);
joystickButton2.whenPressed(new Drive1());
will create the Drive1 command when THIS code is executed, but it won't be SCHEDULED until the button is pressed. And this is the correct way of doing it. Otherwise, the command would be created every time the button is pressed and that would be not make the memory allocator happy.
Brad