Language: EN

libreria-arduino-stopwatch

Arduino Stopwatch Library

The Stopwatch library allows you to record the elapsed time in code execution, obtaining the result as elapsed milliseconds or as frequency in Hz. With this library, we can measure time easily, freeing the main flow from these functions.

The Stopwatch class is useful, for example, for making encoders, a tachometer, or determining the frequency of a signal. In addition, we can have multiple instances running simultaneously, and combine it with other libraries like Debounce Filter or Hysteresis.

The Stopwatch class has two operating modes. One is through the use of the Reset() and Update() functions, which respectively initializes the starting time of the measurement and records the time elapsed. Therefore, the recorded time is the time between the call to Reset() and Update().

The other operating mode is through the use of the Measure() function, which takes a void(*)() function as a parameter to measure. Stopwatch records the time taken for the execution of the function passed as a parameter.

In either case, the recorded time is obtained by GetElapsed() and GetFrequency() which respectively obtain the time in milliseconds and the frequency in Hz.

User Manual

Constructor

The Stopwatch class is instantiated through its constructor.

Stopwatch();

Using Stopwatch

// Records the current instant as the start of the measurement
void Reset();

// Records the current instant as the end of the measurement
void Update();

// Records the measurement of the function passed as a parameter
void Measure(StopwatchAction action);

// Gets the elapsed time in milliseconds
unsigned long GetElapsed() const;

// Gets the elapsed time as frequency in Hz
float GetFrequency() const;

Examples

The Stopwatch library includes the following examples to illustrate its use.

  • ResetUpdate: Example of use using Reset() and Update()
  • GetMeasure: Example of use using GetMeasure()

Installation

  • Download the latest version from GitHub
  • Unzip the file
  • Copy to your libraries folder (usually My Documents\Arduino\libraries)
  • Restart the Arduino IDE

github-full