Skip to content

Timer.h

Brandon W. Kipp edited this page Feb 13, 2020 · 1 revision

This library is used to handle common timer-related functions. Currently, it does timing by counting from millis() although, in the future, micros() may be implemented.

To setup a new Timer, you'll need to pass in two parameters:

  1. void (*callback)(boolean, boolean, unsigned long), a callback function that accepts the returned int value of the sensor
  2. unsigned long, the duration of the timer

Timer Methods

Method Arguments Returns Description
postpone (unsigned long postponeAmount) void Resets the timer's duration to postponeAmount, provided in ms.

Timer Example

#include "Libraries/Button.h"
#include "Libraries/Timer.h"

Button button1;
Timer timer1;

void setup() {

  timer1.setup([](boolean running, boolean ended, unsigned long timeElapsed) {
    if (running == true) {
      // Let's make sure a light is on!
    }

    if (ended == true) {
      // Time's up! Lights out.
    }

  }, 3000);

  button1.setup(button1Pin, [](int state) {
    if (state == 1) {

      if (timer1.isRunning() == false) {
        timer1.start();
      }
      else {
        timer1.postpone(3000);
      }
    }
  });
}

void loop() {
  timer1.update();
}

Clone this wiki locally