Timer#

Introduction#

The timer class is derived from the brain base class and provides functionality for measuring elapsed time, allowing you to track durations and schedule time-based events within a VEX V5 program.

Access#

Brain.Timer

Notes#

  • Brain.Timer is an instance of the timer class and provides the same functionality.

Example#

/* This constructor is required when using VS Code.
A Brain is generated automatically at the start of
VEXcode projects. */

// Create the V5 Brain
brain Brain = brain();

// Display the timer as it ticks up
while (true) {
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Time: %.2f", Brain.Timer.time());

  wait(50, msec);
}

Member Functions#

The timer class includes the following member functions:

  • clear — Resets the timer to zero.

  • time — Returns the elapsed time since the timer started.

  • event — Calls a function after a specified number of milliseconds, with optional arguments.

Before calling any sdcard member functions, a brain instance must be created, as shown below:

// Create the V5 Brain
brain Brain = brain();

clear#

Resets the timer’s current value to zero. Afterwards, the timer immediately begins counting up again from zero.

Available Functions
void clear();

Parameters

This function does not accept any parameters.

Return Values

This function does not return a value.

Examples
while (true) {
  // Reset the timer every time the screen is pressed
  if (Brain.Screen.pressing()) {
    Brain.Timer.clear();
  }
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Time: %.2f", Brain.Timer.value());
  wait(50, msec);
}

time#

Returns the current value of the timer.

Available Functions
1
uint32_t time() const;
  • Returns the elapsed time in milliseconds
2
double time(
  timeUnits units ) const;
  • Returns the elapsed time in the specified units
Parameters

Parameter

Type

Description

units

timeUnits

The units used to return the elapsed time:

  • seconds / sec — seconds
  • msec — milliseconds

Return Values
  • The parameterless function (1) returns a uint32_t representing the elapsed time in milliseconds.

  • The function with a timeUnits parameter (2) returns a double representing the elapsed time in the specified units.

Notes
  • The timer begins counting when the project starts.

  • Calling clear resets the timer value to zero.

Examples
// Display the timer as it ticks up
while (true) {
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Time: %.2f", Brain.Timer.time());

  wait(50, msec);
}

event#

Registers a callback function to be executed after a specified delay.

Available Functions
1
static void event(
  void (*callback)(void*),
  uint32_t value );
  • Registers a callback to run after a specified delay
  • Allows a user-defined value to be passed to the callback
2
static void event(
  void (*callback)(void),
  uint32_t value );
  • Registers a callback to run after a specified delay
Parameters

Parameter

Type

Description

callback

void ()(void)

A function that will be called after the specified delay and receives a user-defined argument.

callback

void (*)(void)

A function that will be called after the specified delay.

value

uint32_t

The delay, in milliseconds, before the callback function is executed.

Return Values

This function does not return a value.

Notes
  • This function schedules a one-time callback; it does not repeat automatically.

  • The callback function must return void.

  • Multiple events may be scheduled at the same time.

Examples
// Create a function to turn the screen orange
void turnOrange() {
  Brain.Screen.clearScreen(orange);
}

// Call turnOrange after 2 seconds
Brain.Timer.event(turnOrange, 2000);