I am writing a timing library. (Well I am playing around)
Can you think of other functions that could be useful to add?
#include <CheckTime.h>
//Instantiate objects
CheckTime MyTimer;
CheckTime My2ndTimer;
CheckTime My3rdTimer;
CheckTime FlashLED;
CheckTime Flash2ndLED;
void setup()
{
Serial.begin(9600);
MyTimer.StartTiming(5000UL); //Start this timer with 5 second delay
FlashLED.StartTiming(100UL); //Start this timer with 1/10 second delay
My2ndTimer.StartTiming(6000UL); //Start this timer with 6 second delay
Flash2ndLED.StartTiming(500UL); //Start this timer with 1/2 second delay
My3rdTimer.StartTiming(10000UL); //Start this timer with 10 second delay
pinMode(13,OUTPUT); //LED connected to this pin
pinMode(12,OUTPUT); //LED connected to this pin
pinMode(11,OUTPUT); //LED connected to this pin
pinMode(10,OUTPUT); //LED connected to this pin
}
void loop()
{
//Ex: #1, limits how long something is done before a delay period is up
if(MyTimer.DoUntil()) //As long as we have not reached the delay time, execute the code within
{
if(FlashLED.Repeat()) //When this delay is over execute the code within, then start again.
{
digitalWrite(13,!digitalRead(13));
}
}
//Ex: #2, executes something once after a delay period
if(My2ndTimer.DoWhen()) //When the timer delay is over, execute the code within
{
Serial.println("Time is UP");
digitalWrite(12,!digitalRead(12)); //Toggle LED on then off
delay(1); //so you can see the LED flash
digitalWrite(12,!digitalRead(12));
}
//Ex: #3, executes something after a delay period then start the delay period again
if(My3rdTimer.DoWhen()) //When the timer delay is over, execute the code within
{
digitalWrite(11,!digitalRead(11));
My3rdTimer.StartTiming(10000UL); //Restart this delay period
delay(1); //so you can see the LED flash
digitalWrite(11,!digitalRead(11));
}
//Ex: #4, executes something over and over again after a delay period
if(Flash2ndLED.Repeat()) //When the delay is over, execute the code within, then start again.
{
digitalWrite(10,!digitalRead(10));
}
} //END of loop()
#include <Arduino.h>
// CheckTime.cpp
// CheckTime.h"
//
// Rev 1.00 October 3, 2015 functional code
//
//=============================================================================
// F U N C T I O N P R O T O T Y P E S
//=============================================================================
// C l a s s C h e c k T i m e
class CheckTime
{
public:
//CheckTime();
void begin();
void StartTiming(unsigned long);
void ResetTiming();
void EnableTiming();
void DisableTiming();
void SetDelay(unsigned long);
bool IsEnabled();
bool Repeat();
bool DoWhen();
bool DoUntil();
private:
bool enabled;
unsigned long _Delay;
unsigned long lastMillis;
}; // END of CheckTime class
// In C++ you must have a ; at the end of a class definition
//*****************************************************************************
#include "CheckTime.h"
//
//*****************************************************************************
// C l a s s C h e c k T i m e
//*****************************************************************************
//Constructor
//CheckTime::CheckTime()
//{
//} // END of CheckTime()
//**********************************************************
//Initialization
//void CheckTime::begin()
//{
//} // END of begin()
//**********************************************************
//
void CheckTime::StartTiming(unsigned long Delay)
{
_Delay = Delay;
lastMillis = millis();
enabled = true;
} // END of StartTimer()
//**********************************************************
//
void CheckTime::EnableTiming()
{
enabled = true;
} // END of EnableTimer()
//**********************************************************
//
void CheckTime::DisableTiming()
{
enabled = false;
} // END of DisableTimer()
//**********************************************************
//
bool CheckTime::IsEnabled()
{
return enabled;
} // END of IsEnabled()
//**********************************************************
//
void CheckTime::ResetTiming()
{
lastMillis = millis();
} // END of ResetTimer()
//**********************************************************
//
void CheckTime::SetDelay(unsigned long Delay)
{
_Delay = Delay;
} // END of SetDelay()
//**********************************************************
//
bool CheckTime::Repeat()
{
if(enabled && millis() - lastMillis >= _Delay)
{
lastMillis = lastMillis + _Delay;
return true;
}
return false;
} // END of Repeat()
//**********************************************************
//
bool CheckTime::DoWhen()
{
if(enabled && millis() - lastMillis >= _Delay)
{
enabled = false;
return true;
}
return false;
} // END of DoWhen()
//**********************************************************
//
bool CheckTime::DoUntil()
{
if(enabled && millis() - lastMillis <= _Delay)
{
return true;
}
enabled = false;
return false;
} // END of DoUntil()
//**********************************************************