Timer#
Introduction#
The VEX AIM Coding Robot’s timer keeps track of elapsed time from the start of a project. It can be used to measure durations, trigger events after a set time, or reset for new timing operations. Below is a list of all available methods:
Action – Control the timer.
reset – Resets the timer to zero.
Getter – Return the current timer value.
time – Returns the elapsed time since the project started.
Callback – Trigger functions after a delay.
event – Calls a function after a specified number of milliseconds, with optional arguments.
Constructor - Create a Timer to track time.
Timer - Create a new timer object that can be used with these methods.
Action#
reset#
reset
sets the timer to zero.
Usage:
timer.reset()
Parameters |
Description |
---|---|
This method has no parameters. |
# Display how long a turn takes
robot.move_for(100, 0)
timer.reset()
robot.turn_for(RIGHT, 90)
robot.screen.print("Time elapsed:")
robot.screen.next_row()
robot.screen.print("%d seconds" % timer.time(SECONDS))
Getter#
time#
time
returns the current elapsed time of the timer in the specified units — an integer for MSEC
or a float for SECONDS
.
Usage:
timer.time(units)
Parameters |
Description |
---|---|
|
The time units are milliseconds |
# Print the time in seconds
# and in milliseconds on the next row.
while timer.time(SECONDS) <= 5:
robot.screen.clear_screen()
robot.screen.set_cursor(1, 1)
robot.screen.print("Time: %d" % timer.time(MSEC))
robot.screen.next_row()
robot.screen.print("Time: %.2f" % timer.time(SECONDS))
wait(50, MSEC)
Callback#
event#
event
calls a function after a specified amount of time.
Usage:
timer.event(callback, delay, arg)
Parameters |
Description |
---|---|
|
A function to execute when the timer event occurs. |
|
The delay before the function is called, in milliseconds. |
|
Optional. A tuple containing arguments to pass to the callback function. See Using Events with Parameters for more information. |
# Create a function to play a sparkle sound
def timer_callback():
robot.sound.play(SPARKLE)
# Call the function after 2000 milliseconds (2 seconds)
timer.event(timer_callback, 2000)
Constructor#
Timer#
A new timer can be created using the Timer
constructor. A new timer starts measuring time immediately when it is created.
Usage:
Timer()
Parameter |
Description |
---|---|
This constructor has no parameters. |
## Display 2 timers with a 2 second difference
wait(2, SECONDS)
stopwatch = Timer()
while True:
robot.screen.clear_screen()
robot.screen.set_cursor(1, 1)
robot.screen.print("Timer:")
robot.screen.set_cursor(2, 1)
robot.screen.print(timer.time(SECONDS))
robot.screen.set_cursor(4, 1)
robot.screen.print("Stopwatch:")
robot.screen.set_cursor(5, 1)
robot.screen.print(stopwatch.time(SECONDS))
wait(0.1, SECONDS)