Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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.

Note

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

Expand
title#1: Perform an action each 10 ms
Code Block
languagecpp
#include <CrcLib.h>

using namespace Crc;

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
    ...
    */
}
Expand
title#2: Perform an action 1s after a button press (advanced)
Code Block
languagecpp
#include <CrcLib.h>

using namespace
Crc;

void setup() {
    CrcLib::Initialize();

    /* 
    The rest of your setup code
    ...
     */
}

CrcLib::Timer timer;
bool waiting = false;
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 (!waitingtimer.IsWaiting() && buttonPressed) {
        // Start the timer with a period of 1000 ms (1s)
        timer.Start(1000);
        waiting
= true; // We are now waiting
    }
    
    // If wethe were1s waitingtimer and 1s has elapsed
    if (waiting && timer.IsFinished()) {
        // We are no longer waiting
        waiting = false;
        
        // Do what you wanted to do after 1s here
        // ...
    }
    
    /* 
    The rest of your looping code
    ...
    */
}
Filter by label (Content by label)
sorttitle
excerptTyperich content
cqllabel = "ar-crclib_funct_utilities"