ElapsedTime - Multiple Movements

Right now our robot should move forward 3 seconds then stop. What if we wanted our robot to do something else after those 3 seconds? How do we request our program to continue?

To save some time we can copy and paste our entire loop and timer reset below our existing code to make adjustments to!

runtime.reset();
while (opModeIsActive() && (runtime.seconds() <= 3.0)) {
    leftmotor.setPower(1);
    rightmotor.setPower(1);
    telemetry.addData("Number of Seconds in Phase 1", runtime.seconds());
    telemetry.update();
        }

runtime.reset();
while (opModeIsActive() && (runtime.seconds() <= 3.0)) {
    leftmotor.setPower(1);
    rightmotor.setPower(1);
    telemetry.addData("Number of Seconds in Phase 1", runtime.seconds());
    telemetry.update();
        }

Notice our second loop also has a call for telemetry data, however the name is the same! Let's edit it to be "Number of Seconds in Phase 2". Keep the names in mind if you duplicate additional loops.

Quick Check!

Give your program a test to see what happens. Think about the following while testing:

  • How long does the robot move?

  • Could you tell when the robot switched between Phase 1 and 2?

  • What happens if we change the power in the second loop?

Reversing Movement

Having multiple loops with different amounts of time can give us a lot of power to help our robot navigate an area. For now let's have our robot complete it's first movement forward for 3 seconds, then reverse back to the start.

This simply requires changing our power in the second loop to -1 !

runtime.reset();
while (opModeIsActive() && (runtime.seconds() <= 3.0)) {
    leftmotor.setPower(1);
    rightmotor.setPower(1);
    telemetry.addData("Number of Seconds in Phase 1", runtime.seconds());
    telemetry.update();
        }

runtime.reset();
while (opModeIsActive() && (runtime.seconds() <= 3.0)) {
    leftmotor.setPower(-1);
    rightmotor.setPower(-1);
    telemetry.addData("Number of Seconds in Phase 2", runtime.seconds());
    telemetry.update();
        }

Last updated