I need a motor to run for a short duration when I click a button. I already have it working to run continuously when I hit the button but im not sure how to get it to only run for like half a second or something.
I’m sure this is a simple fix I’m just a rookie programmer so could anyone walk me through this?
FYI, I’m using the command based template from WPI.
If you don’t want something big and fancy like ^ that guy suggested :), I would use the FPGA time method in the Utility class.
long millisecondsToRun = 1000; // This should run 1000ms = 1 s.
long initTime = Utility.getFPGATime();
while (Utility.getFPGATime() - initTime <= millisecondsToRun){
// Place your code here.
}
What this does is:
Utility.getFPGATime() gives you a time in milliseconds, say 123400 ms. Now, you want to run your code for a continuous 2 seconds. You would then set the while loop to run your code UNTIL 2 seconds or 2000 ms has passed. So, the loop would be to run until the FPGA time is equals or greater than 125400 ms. And then, it stops running because the conditional is no longer true.
You could try this. This worked for me when I had your EXACT problem.
public void autonomous()
{
Timer.start() //starts a timer at 0 seconds
while(isAutonomous() && isEnabled()) //run when autonomous is Clive and you are enabled
{
if(Timer.get() < time) //time is a double in terms of seconds
{
driveVariable.drive(speed, curve); //move robot at this speed with/without curve
}
Timer.delay(0.005); //does nothing for 5 seconds but helps refresh motors in loop
}
Timer.stop() //stops timer
}