# Best timer library

**URL:** <https://forum.arduino.cc/t/best-timer-library/944347>\
**Category:** Libraries\
**Created:** [January 7, 2022, 3:35pm UTC](https://forum.arduino.cc/t/best-timer-library/944347 "2022-01-07T15:35:01Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [January 7, 2022, 6:50pm UTC](https://forum.arduino.cc/t/best-timer-library/944347/7 "2022-01-07T18:50:50Z")

</div>

A **delay library** would do the same as using the millis() technique in non blocking delays.

* * *

A library does speed implementation.

If you need a library, this is one I use often.

Lots of other versions are easily found.

```cpp

// M A C R O S
// **********************************************************************
//
//Define some MACROS:
//
//1. LED wiring where a HIGH on the pin turns the LED on
#define LEDon HIGH
#define LEDoff LOW

#define PUSHED LOW
#define notPUSHED HIGH

// c l a s s m a k e T i m e r
//======================================================================
//
class makeTimer
{
#define MILLIS true
#define MICROS false

    //Note: since code takes time to execute, precise micro second timing is difficult.

    //StartTime = the time this "Timer" was (re)started
    //Interval = Interval/delay time we are looking for
    //Restart = do we start "this timer" again and again
    //EnableFlag = is "this timer" enabled/allowed to be accessed
    //TimerType = true = millis(), false = micros()
    // **********************
    //For each timer object you need:
    //Example:
    // makeTimer myTimer = //create a Timer named "myTimer"
    // {
    // 0, 200UL, true, true, MILLIS //StartTime, Interval, Restart, EnableFlag, TimerType (MILLIS/MICROS)
    // };
    //
    //Each Timer object is made up of 5 variables:
    //myTimer.StartTime, myTimer.Interval, myTimer.Restart, myTimer.EnableFlag and myTimer.TimerType
    //
    //You have the following class functions:
    //myTimer.CheckTime() and myTimer.EnableTimer() and myTimer.DisableTimer()
    // **********************

  public:
    unsigned long StartTime;
    unsigned long Interval;
    bool Restart;
    bool EnableFlag;
    bool TimerType;

    unsigned long currentTime;

    // m a k e t i m e r ( )
    // **********************************************************************
    //Constructor
    //Note: If you do not have this constructor, the compiler will make this for you.
    //
    makeTimer(unsigned long ST, unsigned long INT, bool RES, bool ENA, bool TT)
    {
      StartTime = ST;
      Interval = INT;
      Restart = RES;
      EnableFlag = ENA;
      TimerType = TT;

    } //END of m a k e t i m e r ( )

    // C h e c k T i m e r ( )
    // **********************************************************************
    //Delay time expired function "CheckTime()"
    //
    bool CheckTime()
    {
      //StartTime = the time TimerX was (re)started
      //Interval = interval/delay we are looking for
      //Restart = do we restart TimerX automatically
      //EnableFlag = is TimerX enabled/allowed to be accessed
      //TimerType = use ms or us timing, MILLIS/true = millis(), MICROS/false = micros()

      if (TimerType == MILLIS)
      {
        //using millis() for this Timer
        currentTime = millis();
      }
      else
      {
        //using micros() for this Timer
        currentTime = micros();
      }

      //Is Timer enabled and has the Timer expired?
      if (EnableFlag == true && currentTime - StartTime >= Interval)
        //Note: if delays of < 2 millis are needed use micros() and adjust 'Interval' as needed
      {
        //Should we restart the Timer automatically?
        if (Restart)
        {
          //get ready for the next iteration
          StartTime = currentTime;
        }

        //This Timer did expired
        return true;
      } //END of if(EnableFlag == true && currentTime - StartTime >= Interval)

      //This timer did not expire or it is disabled
      return false;

    } //END of C h e c k T i m e r ( )

    // E n a b l e T i m e r ( )
    // **********************************************************************
    //Function to enable and initialize a timer, ex: myTimer.EnableTimer();
    //
    void EnableTimer()
    {
      EnableFlag = true;

      //initialize lastTime to current millis() or micros()
      if (TimerType == true)
      {
        StartTime = millis();
      }
      else
      {
        StartTime = micros();
      }

    } //END of E n a b l e T i m e r ( )

    // D i s a b l e T i m e r ( )
    // **********************************************************************
    //Function to enable and initialize a timer, ex: myTimer.EnableTimer();
    //
    void DisableTimer()
    {
      EnableFlag = false;

    } //END of D i s a b l e T i m e r ( )

}; //END of c l a s s m a k e T i m e r

// **********************************************************************
// Create timer objects and initialize them in this sketch
// **********************************************************************
makeTimer heartbeatLED = //create TIMER heartbeatLED
{
  0, 200UL, true, true, MILLIS //StartTime, Interval, Restart, EnableFlag, TimerType (MILLIS/MICROS)
};

// ***************************
makeTimer checkSwitches = //create TIMER checkSwitches
{
  0, 50UL, true, true, MILLIS //StartTime, Interval, Restart, EnableFlag, TimerType (MILLIS/MICROS)
};

// ***************************
//makeTimer whatEver = //create TIMER whatEver
//{
// 0, 0UL, false, false, MILLIS //StartTime, Interval, Restart, EnableFlag, TimerType (MILLIS/MICROS)
//};
// ***************************

// **********************************************************************
// G l o b a l v a r i a b l e s
// **********************************************************************
//inputs
const byte mySwitch = 2; //switch is on Arduino pin 2, pushed = LOW

//outputs
const byte heartbeatLEDpin = 13;
const byte switchLEDpin = 12;

//SRAM variables
const unsigned long myLongPress = 2000UL;// 2 seconds
const unsigned long myShortPress = 500UL; // 1/2 second

byte lastSwitchState;

// s e t u p ( )
// **********************************************************************
//
void setup()
{
  pinMode(heartbeatLEDpin, OUTPUT);
  pinMode(switchLEDpin, OUTPUT);

  pinMode(mySwitch, INPUT_PULLUP);  
  
} //END of setup()

// l o o p ( )
// **********************************************************************
//
void loop()
{
  // **********************************
  //Heartbeat LED
  //every 200ms or 500ms
  if (heartbeatLED.CheckTime())
  {
    //toggle heartbeatLED
    digitalWrite(heartbeatLEDpin, !digitalRead(heartbeatLEDpin));
  }

  // **********************************
  //is it time to check the switches ?
  //every 50ms
  if (checkSwitches.CheckTime())
  {
    serviceSwitches();
  }

  // **********************************
  //other none blocking code goes here
  // **********************************

} //END of loop()

// s e r v i c e S w i t c h e s ( )
// **********************************************************************
//
void serviceSwitches()
{
  // **********************************
  byte switchState = digitalRead(mySwitch);

  //has there been a change in switch state ? m y S w i t c h
  if (lastSwitchState != switchState)
  {
    //updater to the new state
    lastSwitchState = switchState;

    //did the switch get pushed ?
    if (switchState == PUSHED)
    {
      //toggle the LED on pin 12
      digitalWrite(switchLEDpin, !digitalRead(switchLEDpin));

      //just for fun, change the rate the heartbeatLED flashes
      heartbeatLED.Interval = 500ul;

    }

    //the switch was released
    else
    {
      //the switch is released, do something 
      
      //just for fun, change the rate the heartbeatLED flashes
      heartbeatLED.Interval = 200ul;
      
    }

  } // END of mySwitch code

  // **********************************
  //next switch code
  
} //END of serviceSwitches()

```

---

_[View the full topic](https://forum.arduino.cc/t/best-timer-library/944347)._
