Timer

A timer digital source unit that counts time. The timer can be started, stopped, and resumed.

When started, the timer goes from 0.0 to 1.0 (ie. 0% to 100%) through its duration.

Example

Uses a timer to change the duty cycle of a blinking LED, then restarts with a new random duration.

#include <Plaquette.h>

Timer myTimer(2.0); // a timer with 2 seconds duration

DigitalOut led(13);

SquareOsc osc(3.0); // a square oscillator with a 3 seconds period

void begin() {
  myTimer.start(); // start timer
}

void step() {
  // Adjust oscillator's duty cycle according to current timer progress.
  osc.dutyCycle(myTimer);

  // Apply oscillator to LED state.
  osc >> led;

  if (myTimer.isFinished()) // if the timer has completed its course
  {
    // Restarts the timer with a random duration between 1 and 5 seconds.
    myTimer.start(randomFloat(1.0, 5.0));
  }
}

Reference

class Timer : public Node, public AbstractTimer

Chronometer class which ramps from 0.0 to 1.0 in a given duration.

Public Functions

virtual float get()

Returns progress in [0, 1].

virtual void start()

Starts/restarts the chronometer.

virtual void start(float duration)

Starts/restarts the chronometer with specific duration.

inline virtual bool isFinished() const

Returns true iff the chronometer has completed its process.

virtual void stop()

Interrupts the chronometer.

virtual void resume()

Resumes process.

inline virtual float elapsed() const

The time currently elapsed by the chronometer (in seconds).

inline bool isStarted() const

Returns true iff the chronometer is currently running.

See Also