Skip to content

MrDev-5000/TimerKernel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⏱️ TimerKernel

PlatformIO Registry License GitHub issues

TimerKernel is a lightweight Arduino library for managing non-blocking timers


🚀 Features

  • Non-Blocking, No delay(), for best code responsiveness
  • Support for multiple time units (MINUTE, SECOND, MILLISECOND, MICROSECOND)
  • Multiple independent timers can be used simultaneously
  • Simple and intuitive API
  • Supports Floating Point duration for ease of use and percise timing
  • Supports running timer only a stated number of times

⚙️ Installation

To install this library:

  1. Download ZIP
    Click the green Code button on this repository page, then select "Download ZIP".

  2. Open Arduino IDE
    Launch the Arduino IDE on your computer.

  3. Install ZIP Library

    • Go to Sketch > Include Library > Add .ZIP Library...
    • Navigate to where you downloaded the ZIP file.
    • Select the ZIP file and click Open.
  4. Done!
    The library is now installed and ready to use. You can find examples for this library in File > Examples > TimerKernel.

Note:

  • Do not unzip the downloaded file before installing.
  • If you are updating an existing library, delete the old version from your libraries folder first.

🛠️ Usage

Traditionally, using millis() for timed events requires manual tracking of multiple variables:

unsigned long previousMillis_1 = 0; //store time for first event
unsigned long previousMillis_2 = 0; //store time for second event

const long interval_1 = 1000; //interval for first event
const long interval_2 = 2000; //interval for second event


void setup(){

}

void loop() {

//check time since program started, and store in "currentMillis"
unsigned long currentMillis = millis();

   //conditional that checks whether 1 second has passed since last event
   if (currentMillis - previousMillis_1 >= interval_1) {
        previousMillis_1 = millis();
        //execute a piece of code, every *1 second*
   }

   //conditional that checks whether 2 seconds have passed since last event
   if (currentMillis - previousMillis_2 >= interval_2) {
        previousMillis_2 = millis();
        //execute a piece of code, every *2 seconds*
   }
}

With TimerKernel, this becomes much simpler and easier to manage:

#include <TimerKernel.h>

TimerKernel timer_1;
TimerKernel timer_2;

const long interval_1 = 1000; //interval for first event
const long interval_2 = 2000; //interval for second event


void setup(){

}

void loop() {

   //conditional that checks whether 1 second has passed since last event
   if (timer_1.hasExpired(interval_1)) {
       //execute a piece of code, every *1 second*
   }

   //conditional that checks whether 2 seconds have passed since last event
   if (timer_2.hasExpired(interval_2)) {
       //execute a piece of code, every *2 seconds*
   }
}

📖 Examples

  • simple_led_blink - demonstrates how to use toggleState() to blink a led.
  • timed_event - demonstrates how to use hasExpired() to do a task periodically.
  • run_n_times - demonstrates how to run timer only a specific number of times.
  • run_n_times-advanced - demonstrates how to restart timers on button press.

📚 API Reference

hasExpired()

  bool hasExpired(double duration, TimeUnit unit = MILLISECOND, int runAmount = INFINITE);

hasExpired() runs runAmount of times. It checks whether the specified time duration has passed. By default, the duration is expressed in milliseconds.

Parameter Type Description
duration double Time interval in desired unit
unit enum Time unit (MINUTE, SECOND, MILLISECOND, MICROSECOND)
runAmount int Number of times to run hasExpired()

Returns:

  • true if the specified time duration has elapsed and hasExpired() has not yet been executed runAmount times, otherwise returns false .

toggleState()

  bool toggleState(double duration, TimeUnit unit = MILLISECOND, int runAmount = INFINITE);

toggleState() runs runAmount of times. It toggles state every time the duration elapses. By default, the duration is expressed in milliseconds.

Parameter Type Description
duration double Time interval in desired unit
unit enum Time unit (MINUTE, SECOND, MILLISECOND, MICROSECOND)
runAmount int Number of times to run hasExpired()

Returns:

  • The toggled state (true or false) if toggleState() has not yet been executed runAmount times. otherwise returns false.

resetTimer()

  void resetTimer();

Resets both hasExpired() and toggleState(), causing them to start counting from zero. Calling this function restarts the cycles of both timers until runAmount is reached.

Returns:

  • Nothing

resetToggleState()

  void resetToggleState();

Resets toggleState() function, causing it to start counting from zero. Calling this function restarts the toggle cycle until runAmount is reached.

Returns:

  • Nothing

resetHasExpired()

  void resetHasExpired();

Resets hasExpired() function, causing it to start counting from zero. Calling this function restarts the timing cycle until runAmount is reached.

Returns:

  • Nothing

Note:

  • By Default, all durations are interpreted in millisecond.
  • By Default, all timers run infinitely.

📜 License

About

This is a simple and lightweight Arduino library for non-blocking timing functonality using millis() and micros().

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages