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 how long something takes 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 how much time has passed.
Callback – Trigger functions after a delay.
event– Registers a function to be called after a specified number of milliseconds.
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. This can be used to time additional sections of code within the same project.
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 time since the timer was last reset as a decimal. The timer is automatically reset at the start of a project.
Usage:
timer.time(units)
Parameters |
Description |
|---|---|
|
The unit that represents the time: |
# 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 passes.
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#
Timer creates a new timer. A Timer object will immediately begin counting the moment it is created and will work with all timer methods.
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)