ElapsedTime Logic

For now our goal will be to have the motors move forward for 3 seconds. To accomplish this we need to edit our main While Loop so that it triggers when the OpMode is active AND the ElapsedTime timer is less than or equal to 3 seconds.

Setting a Time Limit

Lets start by creating our less than or equal to condition. Grab the from the Logic menu.

Next select the block from the ElapsedTime menu. Snap the block into the left side of the block. Using the dropdown menu, change the generic to the variable we established earlier.

On the other side of our equation, we need to add a block from the Math menu. Change the number block to 3.

Right now the is equal to three. Use the arrow next to the equal sign to choose the less than or equal to sign from the dropdown menu.

With our time condition ready, we can set it aside for a moment.

Modifying Our While Loop

Now let's set up our logic to modify our While Loop.

First, grab an block from the Logic menu. The block we currently have as part of our loop will be moved to the lefthand side.

On the other side, add the block:

This block set will connect where the block originally was. Now the while loop will now activate when both conditions of the AND block are true.

Quick Check!

Let's give our OpMode a try and test the following scenarios:

  • What happens when hitting play quickly after the initialization button is pressed?

  • What happens when hitting play 2 seconds after the initialization button is pressed?

  • What happens when hitting play 10 seconds after the initialization button is pressed?

What happens while testing?

You may notice the robot moves different distances depending on how long the wait is between INITIALIZATION and PLAYING the program. But why is that?

Remember our timer starts counting when created. Currently, our program creates our timer during initialization meaning it's counting up before Play is ever pressed. If we wait too long our robot may not do anything at all when clicking Play!

Resetting the Timer

Not being able to pause between initialization and pressing Play is probably not ideal in most situations. It certainly makes tracking how far the robot will travel more challenging, the opposite of what we'd like ElapsedTime to help us do.

To keep this from happening the timer should be reset once the OpMode is active. Grab the call block from the ElapsedTime menu and switch it to our runtime variable.

This will be added to our program BELOW the comment and ABOVE the while loop.

Since this is before our loop our robot will complete it once when Play is pressed. Then will complete the check for our while loop.

Test your program again with this change!

Adding Telemetry

In previous parts, we've looked at adding telemetry as a way for the robot to communicate with us. In this situation, it would be helpful for the robot to be able to tell us how much time it has counted so we can make adjustments to our program!

Recall we can find our telemetry block under the utilities menu:

Our block will snap into our number slot.

For our key let's call it "Number of Seconds in Phase 1" for now. This will be useful for distinguishing where in our program our robot is during the next section.

Save your OpMode and give it a try!

Last updated

Was this helpful?