Timer {FR}

Purpose

Delay a task in time or make it repeat periodically without blocking the CrcDuino.

When to use

When you want to run something after x ms or each x ms.

Where to use

Inside the loop function, after having declared a variable with the correct type.

The timer needs to retain its value and, as such, should generally be declared in the global scope.

Returns

Timer.Start() and Timer.Next() do not return anything. Timer.IsFinished() returns true if the requested time has elapsed or false if it has not. Timer.IsWaiting() returns true if the requested time has not yet elapsed but a timer was started, false otherwise.

Main prototype and parameters

void CrcLib::Timer::Start(unsigned long delay_ms)

The following parameters must be passed to the function for it to work properly:

  • The delay the timer wants to wait for, in milliseconds.

bool CrcLib::Timer::IsFinished()

The following parameters must be passed to the function for it to work properly:

  • No parameters needed.

bool CrcLib::Timer::IsWaiting()

The following parameters must be passed to the function for it to work properly:

  • No parameters needed.

void CrcLib::Timer::Next()

The following parameters must be passed to the function for it to work properly:

  • No parameters needed.

Overloads

This function does not have any overloads. It can only be used as described by the main prototype.

Examples

#include <CrcLib.h> CrcLib::Timer timer; void setup() { CrcLib::Initialize(); // Start the timer with a period of 10 ms timer.Start(10); /* The rest of your setup code ... */ } void loop() { CrcLib::Update(); if (timer.IsFinished()) { // 10 ms has elapsed timer.Next(); // Start the next period of the timer // Do the logic you want to do each 10 ms } /* The rest of your looping code ... */ }
#include <CrcLib.h> void setup() { CrcLib::Initialize(); /* The rest of your setup code ... */ } CrcLib::Timer timer; void loop() { CrcLib::Update(); bool buttonPressed = CrcLib::ReadDigitalChannel(BUTTON::COLORS_LEFT); // If the left button of the "colors" block is pressed // and we weren't already waiting if (!timer.IsWaiting() && buttonPressed) { // Start the timer with a period of 1000 ms (1s) timer.Start(1000); } // If the 1s timer has elapsed if (timer.IsFinished()) { // Do what you wanted to do after 1s here // ... } /* The rest of your looping code ... */ }

Related articles