All Projects → contrem → Arduino Timer

contrem / Arduino Timer

Licence: bsd-3-clause
Non-blocking library for delaying function calls

Projects that are alternatives of or similar to Arduino Timer

TimerInterrupt
This library enables you to use Interrupt from Hardware Timers on an Arduino, such as Nano, UNO, Mega, etc. It now supports 16 ISR-based timers, while consuming only 1 hardware Timer. Timers' interval is very long (ulong millisecs). The most important feature is they're ISR-based timers. Therefore, their executions are not blocked by bad-behavin…
Stars: ✭ 76 (-42.86%)
Mutual labels:  timer, arduino-library, non-blocking
Duetimer
⏳ Timer Library fully implemented for Arduino DUE
Stars: ✭ 184 (+38.35%)
Mutual labels:  timer, arduino, arduino-library
Tinyspi
Arduino hardware SPI library for ATtiny44/84, 45/85, 461/861, 2313/4313.
Stars: ✭ 134 (+0.75%)
Mutual labels:  arduino, arduino-library
Noodle Synth
A User-friendly Arduino/Teensy/ESP8266 Library to play MIDI files with infinite polyphonic notes with full control over volume, pitch, and . . . music. No shields needed(Just a speaker).
Stars: ✭ 99 (-25.56%)
Mutual labels:  arduino, arduino-library
Loralib
Arduino library for LoRa modules based on SX127x/RFM9x chips
Stars: ✭ 134 (+0.75%)
Mutual labels:  arduino, arduino-library
Tft espi
Arduino and PlatformIO IDE compatible TFT library optimised for the STM32, ESP8266 and ESP32 that supports different driver chips
Stars: ✭ 1,215 (+813.53%)
Mutual labels:  arduino, arduino-library
Fpm
Arduino library for the R30x/ZFMxx/FPMxx optical fingerprint sensors
Stars: ✭ 79 (-40.6%)
Mutual labels:  arduino, arduino-library
Simpledht
Simple, Stable and Fast Arduino Temp & Humidity Sensors for DHT11 and DHT22. http://learn.adafruit.com/dht
Stars: ✭ 111 (-16.54%)
Mutual labels:  arduino, arduino-library
Ramp
Arduino Interpolation Library
Stars: ✭ 72 (-45.86%)
Mutual labels:  arduino, arduino-library
Arduinoxinput
XInput library for USB capable Arduino boards
Stars: ✭ 126 (-5.26%)
Mutual labels:  arduino, arduino-library
Lora Serialization
LoraWAN serialization/deserialization library for The Things Network
Stars: ✭ 120 (-9.77%)
Mutual labels:  arduino, arduino-library
Arduino Scheduler
Portable Cooperative Multi-tasking Scheduler for Arduino
Stars: ✭ 127 (-4.51%)
Mutual labels:  arduino, arduino-library
Adafruit ina219
INA219 Current Sensor
Stars: ✭ 129 (-3.01%)
Mutual labels:  arduino, arduino-library
Ultrasonic
Minimalist library for Ultrasonic Module HC-SR04, PING))) and Seeed SEN136B5B to Arduino
Stars: ✭ 77 (-42.11%)
Mutual labels:  arduino, arduino-library
Beelan Lorawan
A LoRaWAN library for compatible arduino board
Stars: ✭ 87 (-34.59%)
Mutual labels:  arduino, arduino-library
Aunit
Unit testing framework for Arduino platforms inspired by ArduinoUnit and Google Test. Used with AUniter or EpoxyDuino for continuous builds.
Stars: ✭ 73 (-45.11%)
Mutual labels:  arduino, arduino-library
Button2
Arduino Library to simplify working with buttons. It allows you to use callback functions to track single, double, triple and long clicks. It also takes care of debouncing.
Stars: ✭ 109 (-18.05%)
Mutual labels:  arduino, arduino-library
Arduino Plotter
An Arduino library for easy graphing on host computer via serial communication
Stars: ✭ 129 (-3.01%)
Mutual labels:  arduino, arduino-library
Nintendoextensionctrl
Arduino library for communicating with Nintendo extension controllers
Stars: ✭ 67 (-49.62%)
Mutual labels:  arduino, arduino-library
Liquidcrystal pcf8574
A library for driving LiquidCrystal displays (LCD) by using the I2C bus and an PCF8574 I2C adapter.
Stars: ✭ 67 (-49.62%)
Mutual labels:  arduino, arduino-library

arduino-timer - library for delaying function calls

Simple non-blocking timer library for calling functions in / at / every specified units of time. Supports millis, micros, time rollover, and compile time configurable number of tasks.

Use It

Include the library and create a Timer instance.

#include <arduino-timer.h>

auto timer = timer_create_default();

Or using the Timer constructors for different task limits / time resolution

Timer<10> timer; // 10 concurrent tasks, using millis as resolution
Timer<10, micros> timer; // 10 concurrent tasks, using micros as resolution
Timer<10, micros, int> timer; // 10 concurrent tasks, using micros as resolution, with handler argument of type int

Call timer.tick() in the loop function

void loop() {
    timer.tick();
}

Make a function to call when the Timer expires

bool function_to_call(void *argument /* optional argument given to in/at/every */) {
    return true; // to repeat the action - false to stop
}

Call function_to_call in delay units of time (unit of time defaults to milliseconds).

timer.in(delay, function_to_call);
timer.in(delay, function_to_call, argument); // or with an optional argument for function_to_call

Call function_to_call at a specific time.

timer.at(time, function_to_call);
timer.at(time, function_to_call, argument); // with argument

Call function_to_call every interval units of time.

timer.every(interval, function_to_call);
timer.every(interval, function_to_call, argument); // with argument

To cancel a Task

auto task = timer.in(delay, function_to_call);
timer.cancel(task);

To cancel all Tasks

timer.cancel();

Check if a timer is empty - no active Tasks

if (timer.empty()) { /* no active tasks */ }

Get the number of active Tasks

auto active_tasks = timer.size();

Be fancy with lambdas

timer.in(1000, [](void*) -> bool { return false; });
timer.in(1000, [](void *argument) -> bool { return argument; }, argument);

Getting the number of ticks until the next Task

auto ticks = timer.ticks(); // usefull for sleeping until the next task
void loop {
    auto ticks = timer.tick(); // returns the number of ticks
}

Avoiding ticks calculation inside of tick

void loop {
    timer.tick<void>(); // avoids ticks() calculation
}

API

/* Constructors */
/* Create a timer object with default settings:
   millis resolution, TIMER_MAX_TASKS (=16) task slots, T = void *
*/
Timer<> timer_create_default(); // auto timer = timer_create_default();

/* Create a timer with max_tasks slots and time_func resolution */
Timer<size_t max_tasks = TIMER_MAX_TASKS, unsigned long (*time_func)(void) = millis, typename T = void *> timer;
Timer<> timer; // Equivalent to: auto timer = timer_create_default()
Timer<10> timer; // Timer with 10 task slots
Timer<10, micros> timer; // timer with 10 task slots and microsecond resolution
Timer<10, micros, int> timer; // timer with 10 task slots, microsecond resolution, and handler argument type int

/* Signature for handler functions - T = void * by default */
bool handler(T argument);

/* Timer Methods */
/* Ticks the timer forward, returns the ticks until next event, or 0 if none */
unsigned long tick(); // call this function in loop()

/* Calls handler with opaque as argument in delay units of time */
Timer<>::Task
in(unsigned long delay, handler_t handler, T opaque = T());

/* Calls handler with opaque as argument at time */
Timer<>::Task
at(unsigned long time, handler_t handler, T opaque = T());

/* Calls handler with opaque as argument every interval units of time */
Timer<>::Task
every(unsigned long interval, handler_t handler, T opaque = T());

/* Cancel a timer task */
void cancel(Timer<>::Task &task);
/* Cancel all tasks */
void cancel();

/* Returns the ticks until next event, or 0 if none */
unsigned long ticks();

/* Number of active tasks in the timer */
size_t size() const;

/* True if there are no active tasks */
bool empty() const;

Installation

Check out the instructions from Arduino.

OR copy src/arduino-timer.h into your project folder (you won't get managed updates this way).

Examples

Found in the examples/ folder.

The simplest example, blinking an LED every second (from examples/blink):

#include <arduino-timer.h>

auto timer = timer_create_default(); // create a timer with default settings

bool toggle_led(void *) {
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
  return true; // keep timer active? true
}

void setup() {
  pinMode(LED_BUILTIN, OUTPUT); // set LED pin to OUTPUT

  // call the toggle_led function every 1000 millis (1 second)
  timer.every(1000, toggle_led);
}

void loop() {
  timer.tick(); // tick the timer
}

LICENSE

Check the LICENSE file - 3-Clause BSD License

Notes

Currently only a software timer. Any blocking code delaying timer.tick() will prevent the timer from moving forward and calling any functions.

The library does not do any dynamic memory allocation.

The number of concurrent tasks is a compile time constant, meaning there is a limit to the number of concurrent tasks. The in / at / every functions return NULL if the Timer is full.

A Task value is valid only for the timer that created it, and only for the lifetime of that timer.

Change the number of concurrent tasks using the Timer constructors. Save memory by reducing the number, increase memory use by having more. The default is TIMER_MAX_TASKS which is currently 16.

If you find this project useful, consider becoming a sponsor.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].