Timer#

Introduction#

The Timer in VEX EXP allows you to track elapsed time and perform actions based on time intervals. It can be used to measure how long something takes or reset for new timing operations.

This page uses brain.timer as Brain Timer. You can also construct a Timer object to create a new timer and replace it with your own configured name as needed.

Below is a list of all methods:

Methods – Control and interact with the Brain’s timer.

  • clear – Resets the timer to zero.

  • time – Returns how much time has passed.

  • event – Registers a function to be called after a specified number of milliseconds.

Constructor – Create additional timers.

  • Timer – Creates an additional timer.

clear#

clear sets the timer to zero. This can be used to time additional sections of code within the same project.

Usage:
brain.timer.clear()

Parameters

Description

This method has no parameters.

# Reset the timer when the Bumper Switch is pressed
while True:
    print("\033[2J")
    print(brain.timer.time(SECONDS))
    wait(50, MSEC)
    if bumper_switch.pressing():
        brain.timer.clear()

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:
brain.timer.time(units)

Parameters

Description

units

The unit that represents the time: MSEC (default) – milliseconds, or SECONDS

# Display the time until the Bumper Switch is pressed
while True:
    print("\033[2J")
    print(brain.timer.time(SECONDS))
    wait(50, MSEC)
    if bumper_switch.pressing():
        brain.timer.clear()

event#

event calls a function after a specified amount of time passes.

Usage:
brain.timer.event(callback, delay, arg)

Parameters

Description

callback

A function to execute when the timer event occurs.

delay

The delay before the function is called, in milliseconds.

arg

Optional. A tuple containing arguments to pass to the callback function. See Using Functions with Parameters for more information.

def timer_event():
    drivetrain.drive_for(FORWARD, 200, MM)

# Drive forward after a 5000 millisecond delay
brain.timer.event(timer_event, 5000)

Constructors#

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

Parameters

Description

This constructor has no parameters.

# Create a Timer
timer = Timer()