Custom 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:

  • 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 timer member functions, a timer instance must be created, as shown below:

// Create a timer instance
timer MyTimer = timer();

clear#

Sets the timer to 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) {
  // Clear the timer every time the screen is pressed
  if (Brain.Screen.pressing()) {
    MyTimer.clear();
  }
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Time: %.2f", MyTimer.time(msec));
  wait(50, msec);
}

time#

Returns the current value of the timer.

Available Functions

1 Returns the elapsed time in milliseconds.

uint32_t time() const;

2 Returns the elapsed time in the specified time units.

double time(
    timeUnits units ) const;

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 custom timer is constructed.

  • 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", MyTimer.time(sec));

  wait(50, msec);
}

event#

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

Available Functions

1 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 );

2 Registers a callback to run after a specified delay.

static void event(
    void (*callback)(void),
    uint32_t value );

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
MyTimer.event(turnOrange, 2000);