Timer#
Introduction#
The VEX AIR Drone Controller’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 – Report 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 the time the drone takes to climb upwards.
drone.take_off(500)
timer.reset()
drone.climb_for(UP, 500)
controller.screen.print("Time elapsed: ")
controller.screen.print(timer.time(SECONDS))
drone.land()
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 |
# Display the time in two units.
while True:
controller.screen.clear_screen()
controller.screen.set_cursor(1, 1)
controller.screen.print("Time: {} ms".format(timer.time(MSEC)))
controller.screen.next_row()
controller.screen.print("Time: {:.2f} s".format(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 looping sound.
def on_timer():
controller.sound.play(LOOPING)
# Play the sound after 4 seconds (4000 mS).
timer.event(on_timer, 4000)
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:
controller.screen.clear_screen()
controller.screen.set_cursor(1, 1)
controller.screen.print("Timer: ")
controller.screen.print(timer.time(SECONDS))
controller.screen.set_cursor(4, 1)
controller.screen.print("Stopwatch: ")
controller.screen.print(stopwatch.time(SECONDS))
wait(0.1, SECONDS)