Arduino Timer Library

I have made a Timer library that can make callbacks or toggle pin states.

I couldn't find anything quite like it but it seems so obvious that I bet there are others out there.

Anyway - Enjoy!

Example:

#include "Timer.h"

Timer t;
int pin = 13;

void setup()
{
  Serial.begin(9600);
  pinMode(pin, OUTPUT);
  t.oscillate(pin, 100, LOW);
  t.every(1000, takeReading);
}

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

void takeReading()
{
  Serial.println(analogRead(0));
}

nice, would be helpful to me until i learn how to actually set up timers and interrupts :slight_smile:

Si:
I have made a Timer library that can make callbacks or toggle pin states.

I couldn't find anything quite like it but it seems so obvious that I bet there are others out there.

Could be, but I like your approach a lot. Couple comments, millis()-derived times should be unsigned, and if comparisons are made to differences, then overflow issues are avoided, e.g.:

int Event::update()
{
	unsigned long now = millis();
	if (now - lastEventTime >= period)
	{

Also, in the examples on your blog, is there an extra "60" in there?

  delay(10 * 60 * 60 * 1000);
...
  t.pulse(pin, 10 * 60 * 60 * 1000, HIGH); // 10 minutes

Thanks Jack. Useful comments.

I'll update it.

I have updated the library - thanks Jack.

I have also put it all on the Playground Wiki: Arduino Playground - HomePage

Glad to be of some small service :.

I did play with it just a bit last night, didn't attempt to exercise it exhaustively, but what I tried worked very well!

I did play with it just a bit last night, didn't attempt to exercise it exhaustively, but what I tried worked very well!

Good. Thanks.

I now have a project in mind to exercise it properly. A new control unit for my heating system - the old one broke!

I thought this was a cute way to make a binary counter.

#include <Timer.h>

#define RED 7    //1
#define YEL 9    //2
#define GRN 13   //4

Timer t;

void setup(void)
{
    pinMode(GRN, OUTPUT);
    pinMode(YEL, OUTPUT);
    pinMode(RED, OUTPUT);
    t.oscillate(RED, 1000, LOW);
    t.oscillate(YEL, 2000, LOW);
    t.oscillate(GRN, 4000, LOW);
}

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

That is very cool Jack.

Mind if I add it to the examples for the next release?

With due credit of course.

Si:
Mind if I add it to the examples for the next release?

Not at all, feel free. Was just fiddling around and sort of stumbled on the concept, so not a lot of IP involved XD Unfortunately it does get out of sync after a bit as the individual events are managed asynchronously. But there is a lesson there, too.

Some ideas: define constants could make reading easier

#define SEC 1000L
#define MIN (60SEC)
#define HOUR (60
MIN)
#define DAY (24 * HOUR)

int tickEvent = t.every( 2HOUR + 45MIN + 23*SEC, doSomething);

would become almost intuitive

maybe add a simple string parser?

int tickEvent = t.every("2:45:23:000", doSomething);

"0:0:0:75" or "0:75" or "75" would all be 75 millisec. Parse right to left.