Contribution: Useful library to implement non-blocking timers

Hello Arduino people.

In the following I present my libraries which allows you to use non-blocking timers.

  • PascalTimer is a library that implements a very easy and robust timer method, examples added
  • PascalChklong (needs PascalTimer) checks whether a signal (actually an arbitrary function returning a boolean) remained for a given time period. This is useful to check whether e.g. a button was pressed or a PIR motion detector triggered really (and ignore very short electronic faulty pulses).

Please also consider this
Using millis() for timing. A beginners guide

Please share your opinion!

Moderator: Please make this sticky so that this will help more people

Greetings! :slight_smile:

libraries.zip (6.5 KB)

The Programming forum is NOT the place to share your contribution. The Other Software Development section is.

I suggest that you consider publishing your libraries on GitHub. This will make them easier for people to discover and also make it easier for people to contribute fixes or improvements. GitHub accounts are free and it's very easy to use.

If you do this, I recommend that you create a separate repository for each library.

The code is now on GitHub:

Since the libraries are very small, I did not create a separate repository for each of them.

Your code will drift if istimer() is not called every ms*

To reset you should probably use...

_lastupdate += interval;

At least that way it only suffers jitter.

  • In fact given the way millis() works on Arduino it can sometimes increase by more than 1 each tick, so you cannot even achieve that.

pas-calc:
I did not create a separate repository for each of them.

This makes installation more challenging for the users, since they can't use the Arduino IDE's Sketch > Include Libraries > Add .ZIP Library feature to install the library from the .zip file downloaded from GitHub, which is the usual approach.

It also prevents you from adding the library to the Arduino Library Manager index to provide an even easier method for allowing the users to install and update the library.

suggestion:

you should write this

  if (((get_dt() >= interval) || (force)) && (enabled)) {...

the other way around

  if ((enabled) && ((get_dt() >= interval) || force)) {...

this way you don't spend time calling the function and using millis() if the timer is not enabled.

I would also suggest to stay true to types state is a bool so don't do

state = LOW;

, the logic value exists in C++ and is 'false'

I would not use a hardcoded 13 in PascalChklong.cpp

   checkled      = 13;

Your get_check_bool_wait() has a granularity of 10ms because of the useless hardcoded delay in theredelay(10);      // stability.. what stability ? call yield() may be...

really think newbies do need a pulse() function from a library ??

Thanks for the helpful replies.

J-M-L:
suggestion: you should write this
[...]
really think newbies do need a pulse() function from a library ??

These I can completely understand and were very helpful for me.
The pulse function was very useful for me because I often use it in the setup function of Arduino to visualize that the controller (re)started successfully.

So since the project is on Github now, feel free to add / correct these improvements!

pcbbc:
Your code will drift if istimer() is not called every ms*
[...]

This I do not understand. istimer() does NOT have to be called every ms but for example if it is called every 10ms (because of other tasks) and the interval is set to 55ms and it is called after 50ms it will not trigger, then it might be called after 60ms and it will trigger, so the maximal delay is smaller than the interval set on the timer.

Thx I’ll pass.

Those were just for you to consider I don’t plan on using this code

I’ve the belief that beginners need to master those so I would not want to hide any of this and make it feel like so complicated that it needs a library

I see those as code snippets every programmer keeps on the side to copy paste from when they don’t feel just typing it and learners should build theirs.

Having yours to look at can be a source of ideas too, so thanks for sharing