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 start let's duplicate our existing loop. We can right click on a block to duplicate it. In this case, since our block is a loop, it will duplicate everything within the loop.
We can snap our second loop below the original, however something is still missing. If we want our second loop to start we need our timer to first reset! We can add a block between our two loops.
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.
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?
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 !
Before getting started, let's quickly identify where we will find our ElapsedTime blocks. This menu can be found under the Utilities dropdown on our side toolbar.
For this section, let's start by creating a new OpMode named HelloRobot_ElapsedTime using the BasicOpMode sample.
Recall when we created variables while programming our drivetrain. We will be using them again during this part to help with calculations! To start create a variable called runtime.
When using our gamepad, we can actively communicate with our robot while our program runs. Our robot waits for our input and acts accordingly until a different command is issued. However, this may not always be the case, such as during the autonomous period of a FTC competition.
Our robot is smart enough to navigate some on its own, however we need to help it along to know what to look for. Eventually, you could work up to your robot being able to navigate using a camera and machine learning or its IMU to sense direction, but for now let's start with one of the built in features of the SDK: ElapsedTime
What do you think of when you think of a timer? A stopwatch? Your phone? Maybe the timer on a microwave or oven? Timers commonly consist of two main categories: count up and count down. You can think about the differences of these two by a comparison like keeping track of how fast a runner did a 100m dash vs. needing to know how much longer our food should cook.
ElapsedTime is a count up timer. Registering the amount of time elapsed from the start of a set event, like the starting of a stopwatch. In this situation, it is the amount of time passed from when the timer is created or reset within the code.
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.
Lets start by creating our less than or equal to condition. Grab the from the Logic menu.
With our time condition ready, we can set it aside for a moment.
Now let's set up our logic to modify our While Loop.
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?
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.
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!
Consider marking different goals on the floor with tape to practice determining how much time the robot needs to reach it.
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:
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!
From the variable menu, add the block to the OpMode below the comment block.
In order to utilize elements of the ElapsedTime, runtime will act as the ElapsedTime variable. Add the block to the block.
Before moving on to the rest of the ElapsedTime structure lets go ahead and add the motor related blocks. Add to the op mode to the while loop.
Remember that on a drivetrain one of our motors will need to be set to run in reverse to prevent the robot from spinning in place! Add the block under the the block set.
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.
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.
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.
Now let's explore what happens when we change our time limit to different amounts. You can adjust your time limit by changing the 3 in our block to a different number.
Our block will snap into our number slot.