Timer#
Introduction#
The timer class is used to keep time and interact with a timer independent of the Brain’s timer.
Class Constructor#
timer();
Class Destructor#
Destroys the timer object and releases associated resources.
~timer();
Parameters#
This constructor does not accept any parameters.
Examples#
// Create a timer instance
timer myTimer = timer();
Member Functions#
The timer class includes the following member functions:
reset— Resets the timer to zero.value— Returns the elapsed time since the timer started.event— Calls a function after a specified number of milliseconds, with optional arguments.
Before calling any timer member functions, a timer instance must be created, as shown below:
/* This constructor is required to use a
custom timer. Replace the values
as needed. */
// Create a timer instance
timer myTimer = timer();
/* This constructor is required when using VS Code.
A Brain is generated automatically at the start of
VEXcode projects. */
// Create the Brain to use Brain.Timer
brain Brain = brain();
reset#
Sets the timer to zero.
Available Functionsvoid reset();
This function does not accept any parameters.
Return ValuesThis function does not return a value.
Exampleswhile (true) {
// Clear the timer every time the screen is pressed
if (Brain.Screen.pressing()) {
myTimer.reset();
}
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("Time: %.2f", myTimer.value());
wait(50, msec);
}
value#
Returns the current value of the timer.
Available Functionsdouble value();
This function does not accept any parameters.
Return ValuesA double representing the amount of time passed in seconds.
NotesThe timer begins counting when the timer is constructed.
Calling
resetresets 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", myTimer.value());
wait(50, msec);
}
event#
Registers a callback function to be executed after a specified delay.
Available Functions1 — Registers a callback to run after a specified delay and passes a user-defined value to the callback.
static void event( void (*callback)(void *), uint32_t value );
Parameters2 — Registers a callback to run after a specified delay.
static void event( void (*callback)(void), uint32_t value );
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.
Define the function (outside of
int main())// Create a function to turn the screen orange void turnOrange() { Brain.Screen.clearScreen(orange); }Register the function inside
int main()int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); timer myTimer = timer(); // Call turnOrange after 2 seconds myTimer.event(turnOrange, 2000); }