Timer#

Introduction#

The 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 check button is pressed
    if (Brain.buttonCheck.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 calls a function after a specified amount of time.

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.

// Play a sound after 2 seconds
void tadaSound() {
  Brain.playSound(tada);
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  Brain.Timer.event(tadaSound, 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 a new timer after two seconds
  wait(2, seconds);
  timer stopwatch;

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

    Brain.Screen.setCursor(1, 1);
    Brain.Screen.print("Timer:");
    Brain.Screen.newLine();
    Brain.Screen.print("%.3f", Brain.Timer.value());

    Brain.Screen.setCursor(4, 1);
    Brain.Screen.print("Stopwatch:");
    Brain.Screen.newLine();
    Brain.Screen.print("%.3f", stopwatch.value());

    wait(100, msec);
  }
}