{"id":30297122,"url":"https://github.com/contrem/arduino-timer","last_synced_at":"2025-08-17T03:42:16.474Z","repository":{"id":44866446,"uuid":"152291165","full_name":"contrem/arduino-timer","owner":"contrem","description":"Non-blocking library for delaying function calls","archived":false,"fork":false,"pushed_at":"2023-11-08T17:42:05.000Z","size":56,"stargazers_count":321,"open_issues_count":8,"forks_count":49,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-05-01T18:28:40.327Z","etag":null,"topics":["arduino","arduino-library","arduino-timer","concurrent-tasks","delay","non-blocking","timer"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/contrem.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"contrem"}},"created_at":"2018-10-09T17:15:55.000Z","updated_at":"2024-04-29T12:33:23.000Z","dependencies_parsed_at":"2023-01-20T05:12:48.339Z","dependency_job_id":null,"html_url":"https://github.com/contrem/arduino-timer","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/contrem/arduino-timer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/contrem%2Farduino-timer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/contrem%2Farduino-timer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/contrem%2Farduino-timer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/contrem%2Farduino-timer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/contrem","download_url":"https://codeload.github.com/contrem/arduino-timer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/contrem%2Farduino-timer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270803531,"owners_count":24648688,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-17T02:00:09.016Z","response_time":129,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arduino","arduino-library","arduino-timer","concurrent-tasks","delay","non-blocking","timer"],"created_at":"2025-08-17T03:42:15.762Z","updated_at":"2025-08-17T03:42:16.443Z","avatar_url":"https://github.com/contrem.png","language":"C++","readme":"# arduino-timer - library for delaying function calls\n\nSimple *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.\n\n### Use It\n\nInclude the library and create a *Timer* instance.\n```cpp\n#include \u003carduino-timer.h\u003e\n\nauto timer = timer_create_default();\n```\n\nOr using the *Timer* constructors for different task limits / time resolution\n```cpp\nTimer\u003c10\u003e timer; // 10 concurrent tasks, using millis as resolution\nTimer\u003c10, micros\u003e timer; // 10 concurrent tasks, using micros as resolution\nTimer\u003c10, micros, int\u003e timer; // 10 concurrent tasks, using micros as resolution, with handler argument of type int\n```\n\nCall *timer*.**tick()** in the loop function\n```cpp\nvoid loop() {\n    timer.tick();\n}\n```\n\nMake a function to call when the *Timer* expires\n```cpp\nbool function_to_call(void *argument /* optional argument given to in/at/every */) {\n    return true; // to repeat the action - false to stop\n}\n```\n\nCall *function\\_to\\_call* **in** *delay* units of time *(unit of time defaults to milliseconds)*.\n```cpp\ntimer.in(delay, function_to_call);\ntimer.in(delay, function_to_call, argument); // or with an optional argument for function_to_call\n```\n\nCall *function\\_to\\_call* **at** a specific *time*.\n```cpp\ntimer.at(time, function_to_call);\ntimer.at(time, function_to_call, argument); // with argument\n```\n\nCall *function\\_to\\_call* **every** *interval* units of time.\n```cpp\ntimer.every(interval, function_to_call);\ntimer.every(interval, function_to_call, argument); // with argument\n```\n\nTo **cancel** a *Task*\n```cpp\nauto task = timer.in(delay, function_to_call);\ntimer.cancel(task);\n```\n\nTo **cancel** all *Task*s\n```cpp\ntimer.cancel();\n```\n\nCheck if a timer is **empty** - no active *Task*s\n```cpp\nif (timer.empty()) { /* no active tasks */ }\n```\n\nGet the number of active *Task*s\n```cpp\nauto active_tasks = timer.size();\n```\n\nBe fancy with **lambdas**\n```cpp\ntimer.in(1000, [](void*) -\u003e bool { return false; });\ntimer.in(1000, [](void *argument) -\u003e bool { return argument; }, argument);\n```\n\nGetting the number of **ticks** until the next *Task*\n```cpp\nauto ticks = timer.ticks(); // usefull for sleeping until the next task\n```\n```cpp\nvoid loop {\n    auto ticks = timer.tick(); // returns the number of ticks\n}\n```\n\nAvoiding **ticks** calculation inside of **tick**\n```cpp\nvoid loop {\n    timer.tick\u003cvoid\u003e(); // avoids ticks() calculation\n}\n```\n\n### API\n\n```cpp\n/* Constructors */\n/* Create a timer object with default settings:\n   millis resolution, TIMER_MAX_TASKS (=16) task slots, T = void *\n*/\nTimer\u003c\u003e timer_create_default(); // auto timer = timer_create_default();\n\n/* Create a timer with max_tasks slots and time_func resolution */\nTimer\u003csize_t max_tasks = TIMER_MAX_TASKS, unsigned long (*time_func)(void) = millis, typename T = void *\u003e timer;\nTimer\u003c\u003e timer; // Equivalent to: auto timer = timer_create_default()\nTimer\u003c10\u003e timer; // Timer with 10 task slots\nTimer\u003c10, micros\u003e timer; // timer with 10 task slots and microsecond resolution\nTimer\u003c10, micros, int\u003e timer; // timer with 10 task slots, microsecond resolution, and handler argument type int\n\n/* Signature for handler functions - T = void * by default */\nbool handler(T argument);\n\n/* Timer Methods */\n/* Ticks the timer forward, returns the ticks until next event, or 0 if none */\nunsigned long tick(); // call this function in loop()\n\n/* Calls handler with opaque as argument in delay units of time */\nTimer\u003c\u003e::Task\nin(unsigned long delay, handler_t handler, T opaque = T());\n\n/* Calls handler with opaque as argument at time */\nTimer\u003c\u003e::Task\nat(unsigned long time, handler_t handler, T opaque = T());\n\n/* Calls handler with opaque as argument every interval units of time */\nTimer\u003c\u003e::Task\nevery(unsigned long interval, handler_t handler, T opaque = T());\n\n/* Cancel a timer task */\nbool cancel(Timer\u003c\u003e::Task \u0026task);\n/* Cancel all tasks */\nvoid cancel();\n\n/* Returns the ticks until next event, or 0 if none */\nunsigned long ticks();\n\n/* Number of active tasks in the timer */\nsize_t size() const;\n\n/* True if there are no active tasks */\nbool empty() const;\n```\n\n### Installation\n\n[Check out the instructions](https://www.arduino.cc/en/Guide/Libraries) from Arduino.\n\n**OR** copy **src/arduino-timer.h** into your project folder *(you won't get managed updates this way)*.\n\n### Examples\n\nFound in the **examples/** folder.\n\nThe simplest example, blinking an LED every second *(from examples/blink)*:\n\n```cpp\n#include \u003carduino-timer.h\u003e\n\nauto timer = timer_create_default(); // create a timer with default settings\n\nbool toggle_led(void *) {\n  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED\n  return true; // keep timer active? true\n}\n\nvoid setup() {\n  pinMode(LED_BUILTIN, OUTPUT); // set LED pin to OUTPUT\n\n  // call the toggle_led function every 1000 millis (1 second)\n  timer.every(1000, toggle_led);\n}\n\nvoid loop() {\n  timer.tick(); // tick the timer\n}\n```\n\n### LICENSE\n\nCheck the LICENSE file - 3-Clause BSD License\n\n### Notes\n\nCurrently only a software timer. Any blocking code delaying *timer*.**tick()** will prevent the timer from moving forward and calling any functions.\n\nThe library does not do any dynamic memory allocation.\n\nThe 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.\n\nA *Task* value is valid only for the timer that created it, and only for the lifetime of that timer.\n\nChange 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.\n\nIf you find this project useful, [consider becoming a sponsor.](https://github.com/sponsors/contrem)\n","funding_links":["https://github.com/sponsors/contrem"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcontrem%2Farduino-timer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcontrem%2Farduino-timer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcontrem%2Farduino-timer/lists"}