Scheduler

How do I time an action to occur for q second, for example, when press button 1, motor1.set(1) for 1 second.

Given the title of the thread, I’m assuming you’re using the Command framework? Create a Command that instantiates a Timer as a member of the class. In initialize(), reset/start the timer. Return true from isFinished() when timer.get() returns a value greater than 1 (this will stop the command after 1 second). Have execute() run motor1.set(1) and end() and interrupted() run motor1.set(0).

Use a TimedCommand. The execute() method should set the motor speed, and the end() method should stop the motor.

Im coding in iterative

I think I would be in millisecond because every loop in the iterative class are executed every 20ms, I guess.

In that case, what you want to do is create a Timer and boolean in your Robot class. Something like (untested):


class Robot {
  private Timer theTimer = new Timer();
  private boolean executing;

  public void teleopInit() {
    executing = false;
  }

  public void teleopPeriodic() {
    if (joy.getRawButtonPressed(1)) {
      // start running the motor
      executing = true;
      theTimer.reset();
      theTimer.start();
    }

    // stop executing after 1 second has elapsed
    if (executing && theTimer.get() > 1) {
      executing = false;
    }

    // if executing, run motor1, otherwise stop motor1
    if (executing) {
      motor1.set(1);
    } else {
      motor1.set(0);
    }
  }
}

I second this. Simple and clean.