Timer#

Introduction#

The V5 Brain’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 — Return the current timer value.

  • value — Returns the elapsed time since the timer 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:
Brain.Timer.reset();

Parameters

Description

This method has no parameters.

int main() {
  vexcodeInit();
  // Initializing Robot Configuration. DO NOT REMOVE!
  while (true) {
    // Reset the timer every time the screen is pressed
    if (Brain.Screen.pressing()) {
      Brain.Timer.reset();
    }
    Brain.Screen.clearScreen();
    Brain.Screen.setCursor(1, 1);
    Brain.Screen.print("Time: %.2f", Brain.Timer.value());
    wait(50, msec);
  }
}

Getter#

value#

value returns the current elapsed time of the timer in milliseconds as a double.

Usage:
Brain.Timer.value()

Parameters

Description

This method has no parameters.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

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

    wait(50, msec);
  }
}

Callback#

event#

event registers a function to be called once after a specified delay.

Usage:
Brain.Timer.event(callback, delay)

Parameters

Description

callback

A previously defined callback function that is called automatically when the timer expires. The function must match the required callback signature. See Callback Functions for more information.

delay

The amount of time to wait before calling the callback as a double in milliseconds.

Callback Signature:
void callback();

Arguments

Description

This callback function has no arguments.

// Turn the screen orange after 2 seconds
void turnOrange() {
  Brain.Screen.clearScreen(orange);
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  Brain.Timer.event(turnOrange, 2000);
}

Constructor#

Timer#

timer creates a new timer. A new timer will start measuring time immediately when it is created.

Usage:
timer();

Parameter

Description

This constructor has no parameters.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Display both timers after two seconds
  wait(2, seconds);
  timer stopwatch;

  while (true) {
    Brain.Screen.clearScreen();

    // Show built-in timer
    Brain.Screen.setCursor(1, 1);
    Brain.Screen.print("Timer:");
    Brain.Screen.newLine();
    Brain.Screen.print("%.3f", Brain.Timer.value());

    // Show new timer (2 seconds delayed)
    Brain.Screen.setCursor(4, 1);
    Brain.Screen.print("Stopwatch:");
    Brain.Screen.newLine();
    Brain.Screen.print("%.3f", stopwatch.value());

    wait(100, msec);
  }
}