Skip to content

Using Timers in Blocks

Chris Johannesen edited this page Nov 23, 2019 · 22 revisions

This tutorial explains the basic use of Timers, for programming in Blocks. If you're in hurry (no time), skip to the .

Why not Sleep?

Many new Blocks programmers use the Sleep command (MyOpMode.sleep) as their only control of time.

This is a very basic and common use of time, in a linear Op Mode used for Autonomous. It tells the robot to accept no new instructions for the specified time.

But what if other activities need to happen, while that motor runs? In Autonomous, for example, you might need to collect sensor data while the robot moves forward for 3 seconds. In TeleOp, you might need to continue driving with gamepads while a CR servo rotates for 2 seconds. The Sleep command will not allow this; "no new instructions".

Timer Menu

In FTC Blocks, timer commands are found in menu at the left side, under Utilities, Time, ElapsedTime.

In general you will create your own timer, then use pre-made commands or methods to access the contents of that timer. This is not as simple as 'look at the clock' or 'look at the stopwatch' in real life. There are more steps, based on the structure and protections of Object-Oriented Programming (OOP). Java is an OOP language, and Blocks is built on Java.

Create a Timer

The first step is to create a new timer object. In Java, an Object is an instance or example of a Class. FTC Blocks provides a class called ElapsedTime. A class contains variables and methods (commands). Your new timer is an instance of that class, inheriting its characteristics. Here we call the new timer object "myTimer". You could think of it as "My New Stopwatch", although you won't be stopping it.

Set myTimer to new Time.ElapsedTime

This is not the usual Blocks "set" command. Instead of assigning a number to a variable, this Block creates, or instantiates, a new object called myTimer. myTimer is an instance of the class ElapsedTime, which we see here is actually a subclass of a more general class called Time.

As soon as a new timer is created, it begins running from a starting time of zero. However you will probably not rely on that zero start value, because you will use the timer later in your Op Mode, when the time value has changed.

Initialize a Timer

Now suppose your Op Mode is ready to use myTimer, probably sometime after 'waitForStart'. You don't know how much time has passed, so you might want to set the contents of myTimer to a definite starting value. Your choices are: zero, or the current system time (time since the Op Mode was initialized). The simplest choice is zero.

call ElapsedTime.reset timer
Description: Reset the specified elapsed time object.

This Block uses a method called "reset", from the class called ElapsedTime. It resets your new clock myTimer to zero. Your new clock will start running immediately and keep running. All you can do is read its changing time value.

What Time is It?

It is important to understand that "myTimer" is simply the name of the 'stopwatch', not the time value itself. It has the color purple and appears in Variables, but reading "myTimer" as a number will not give you the time.

Instead you must use a built-in method or command, to get or retrieve the contents of myTimer. Here is the simplest way.

← ElapsedTime.Time timer
Description: Return the elapsed time since the last reset of the specified elapsed time object in the resolution of the elapsed time object.

We can read this from right to left. This Block uses a method or command named "Time"; you can consider its real name to be "Get A Timer's Current Time Value". Here the method gets the value from myTimer, and passes, or returns, it to the left. On the left could be a variable to hold that number, or it could be another Block that accepts number input.

The units will be Seconds or Milliseconds, whichever is the current setting for this timer.

Here the variable "currentTime" holds the number of seconds or milliseconds that myTimer has been running, since it was last reset to zero. Finally you have the number you want, but remember that this number is fixed, not changing. It was the time value at the exact moment you checked myTimer.

Here the Telemetry Block builds a message showing "currentTime". Although its value may be updating constantly in a Repeat loop, currentTime is not actually the running clock.

Timer Examples

Here are two common uses of timers:

  1. Loop until a time limit is reached. Think of this as an alarm clock.
  2. Measure the length of time (until an action stops or occurs). Think of this as a stopwatch.

Loop for Time

pseudocode sample Blocks code

Measure Length of Time

pseudocode sample Blocks code

Timer Options

← new ElapsedTime startTime call System.nanoTime
Description: Create a new elapsed time object with seconds resolution and initialize it to the current system time or a value that you plug in in its place.

← new ElapsedTime resolution
Resolution [SECONDS or MILLISECONDS] This version of the standard Block allows you to specify the units of time used by the new timer. The default is seconds.

← ElapsedTime.StartTime timer This method provides, or returns, the original starting time value of this timer.

The units will be Seconds or Milliseconds, whichever is the current setting for this timer.

← ElapsedTime.seconds timer This returns the current time value in Seconds, even if the timer uses Milliseconds.

← ElapsedTime.Milliseconds timer This returns the current time value in milliseconds, even if the timer uses Seconds.

← ElapsedTime.Resolution timer This returns a value indicating whether the timer is set to Seconds or Milliseconds.

call ElapsedTime.log timer label This writes a message to the system log file, for later analysis. The message shows the timer's value, with a custom text label for easier identification in the (very detailed) log file.

← call ElapsedTime.toText timer Returns a text string, converted from the timer's numeric value. This allows combining the value with other text.

Clone this wiki locally