Timer#

Introduction#

The Timer in VEXcode VR 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.

Below is a list of all methods:

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

  • timer_reset – Resets the timer to zero.

  • timer_time – Returns how much time has passed.

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

timer_reset#

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

Usage:
brain.timer_reset()

Parameters

Description

This method has no parameters.

def main():
    # Reset the timer every 3 seconds
    while True:
        brain.clear()
        brain.print(brain.timer.time(SECONDS))
        if brain.timer_time(SECONDS) > 3: 
            brain.timer_reset()
        wait(5, MSEC)

# VR threads — Do not delete
vr_thread(main)

timer_time#

timer_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: SECONDS

def main():
    # Display the current time on the timer
    while True:
        brain.clear()
        brain.print(brain.timer_time(SECONDS))
        wait(50, MSEC)

# VR threads — Do not delete
vr_thread(main)

timer_event#

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

Usage:
brain.timer_event(callback, delay)

Parameters

Description

callback

A function to execute when the timer event occurs.

delay

The delay before the function is called, in milliseconds.

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

def main():
    # Drive forward after a 5000 millisecond delay
    brain.timer_event(timer_event, 5000)

# VR threads — Do not delete
vr_thread(main)