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.Timeris an instance of thetimerclass 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 Functionsvoid clear();
This function does not accept any parameters.
Return ValuesThis function does not return a value.
Exampleswhile (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 |
|
|
| 2 |
|
|
Parameter |
Type |
Description |
|---|---|---|
|
|
The units used to return the elapsed time:
|
The parameterless function (1) returns a
uint32_trepresenting the elapsed time in milliseconds.The function with a
timeUnitsparameter (2) returns adoublerepresenting the elapsed time in the specified units.
The timer begins counting when the project starts.
Calling
clearresets the timer value to zero.
// 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 |
|
|
| 2 |
|
|
Parameter |
Type |
Description |
|---|---|---|
|
|
A function that will be called after the specified delay and receives a user-defined argument. |
|
|
A function that will be called after the specified delay. |
|
|
The delay, in milliseconds, before the callback function is executed. |
This function does not return a value.
NotesThis 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.
// Create a function to turn the screen orange
void turnOrange() {
Brain.Screen.clearScreen(orange);
}
// Call turnOrange after 2 seconds
Brain.Timer.event(turnOrange, 2000);