This is an Arduino library for managing event timing. It provides relatively intuitive tools for creating non-blocking timing events in your code.
This library consists of a collection of classes:
Timer: counts up in milliseconds, can be resetOnDelay: turns an output on a set number of milliseconds after an input is turned on.OffDelay: turns an output off a set number of milliseconds after an input is turned off.Debounce: is essentially a combination of bothOnDelayandOffDelay; it can be used to debounce an input.SquareWave: generates a square wave with a set period and duty cycle.Edge: detects the rising and falling edge of an input.LinearRamp: a software linear ramp generator.
This library should work on any microcontroller board. It relies on millis() and abs() from the Arduino API/Language, but has no other dependencies.
- TimerExample
- OnDelayExample
- OffDelayExample
- DebounceExample
- SquareWaveExample
- EdgeExample
- LinearRampExample
update()
Updates the time for all classes in this library except
Edge.Edgeis event based, not time based. This is usually run once at the beginning ofloop().
AutomationTimers.update()#include <AutomationTimers.h> void setup() { // run setup stuff here } void loop() { AutomationTimers.update(); // run other loop stuff here }
Timer
A
Timerobject acts like a read-onlyunsigned longthat always counts up in milliseconds. It can be reset to 0 using thereset()method.The value of the
Timeris prevented from overflowing; once the timer reaches the highest value anunsigned longcan hold, it will stay there until reset.#include <AutomationTimers.h> Timer myTimer; void setup() { pinMode(2, INPUT_PULLUP); Serial.begin(9600); } void loop() { // AutomationTimers.update() is what actually updates the timer value. // It should be run once per loop. // It only needs to be run once, even when using multiple Timer objects. AutomationTimers.update(); // If pin 2 is HIGH, the timer will be reset to 0, so the timer only counts up when pin 2 is LOW. if (digitalRead(2)) myTimer.reset(); // This will print the timer value in milliseconds. Serial.println(myTimer); delay(50); }Timer operator
Returns the value of the timer in milliseconds.
Data type:
unsigned long.if (myTimer >= 2000) { // do something }reset()
Resets the timer to 0.
if (myTimer >= 2000) { myTimer.reset(); // do something else }
Timeris utilized in theOnDelay,OffDelay,Debounce, andLinearRampclasses.
OnDelay
INPUT: ___/""""""""""""""""""\___ | OUTPUT: ___|_________/""""""""\___ | | |<-DELAY->|OnDelay constructor
Creates an
OnDelayobject.
OnDelay(delay)
delay: the delay in milliseconds to wait before setting the outputtrue. Allowed data type:unsigned long.OnDelay myOnDelay(1000);OnDelay operator
Returns the value of the output.
Data type:
bool.if (myOnDelay) { // do something }update()
Updates the input of an
OnDelayobject.
myOnDelay.update(input)
myOnDelay: anOnDelayobject.input: Allowed data typebool.The value of the output. Data type:
bool.
Reading the output is optional.
OffDelay
INPUT: ___/""""""""\_____________ | OUTPUT: ___/""""""""|"""""""""\___ | | |<-DELAY->|OffDelay constructor
Creates an
OffDelayobject.
OffDelay(delay)
delay: the delay in milliseconds to wait before setting the outputfalse. Allowed data type:unsigned long.OffDelay myOffDelay(1000);OffDelay operator
Returns the value of the output.
Data type:
bool.if (myOffDelay == false) { // do something }update()
Updates the input of an
OffDelayobject.
myOffDelay.update(input)
myOffDelay: anOffDelayobject.input: Allowed data typebool.The value of the output. Data type:
bool.
Reading the output is optional.
Debounce
INPUT: ___/""""""""""""""""""\_____________ | | OUTPUT: ___|_________/""""""""|"""""""""\___ | | | | |<-DELAY->| |<-DELAY->|Debounce constructor
Creates a
Debounceobject.
Debounce(delay)
delay: the delay in milliseconds to wait before setting the outputtrueand the delay to wait before setting the outputfalse. Allowed data type:unsigned long.Debounce myDebounce(1000);Debounce operator
Returns the value of the output.
Data type:
bool.if (myDebounce) { // do something }update()
Updates the input of an
Debounceobject.
myDebounce.update(input)
myDebounce: aDebounceobject.input: Allowed data typebool.The value of the output. Data type:
bool.
Reading the output is optional.setDelay()
Changes the delay of an
Debounceobject.
myDebounce.setDelay(delay)
myDebounce: aDebounceobject.delay: the delay in milliseconds to wait before setting the outputtrueand the delay to wait before setting the outputfalse. Allowed data type:unsigned long.
SquareWave
Description
Generates a square wave.
OUTPUT: ___/"""""""""""""\______________/""" | | | |<-ON PERIOD->|<-OFF PERIOD->| | | |<-------TOTAL PERIOD------->|
$dutyCycle=\frac{onPeriod}{totalPeriod}$ Methods
SquareWave constructor
Description
Creates an
SquareWaveobject.Syntax
SquareWave(totalPeriod, dutyCycle)SquareWave(onPeriod, offPeriod)Parameters
totalPerid: the total period of the square wave in milliseconds. Allowed data type:unsigned long.dutyCycle: the duty cycle of the squate wave. This should be less than1and greater than0. Allowed data type:float.onPerid: the period square wave istrue/HIGHin milliseconds. Allowed data type:unsigned long.offPerid: the period square wave isfalse/LOWin milliseconds. Allowed data type:unsigned long.Example
SquareWave myFirstSquareWave(2000, 0.5); SquareWave mySecondSquareWave(1000, 1000);SquareWave operator
Description
Returns the value of the output.
Returns
Data type:
bool.Example
digitalWrite(LED_BUILTIN, mySquareWave);
Edge
INPUT: ___/""""""""""\____ RISING: ___/\______________ FALLING: ______________/\___ CHANGE: ___/\_________/\___Edge operator
Returns the value of the input.
Data type:
bool.bool input = myEdge;update()
Updates the input of an
Edgeobject.
myEdge.update(input)
myEdge: anEdgeobject.input: Allowed data typebool.Nothing
rising()
Returns
truewhen a rising edge is detected on the input.
myEdge.rising()
myEdge: anEdgeobject.Data type:
bool.falling()
Returns
truewhen a falling edge is detected on the input.
myEdge.falling()
myEdge: anEdgeobject.Data type:
bool.
LinearRamp
|""""""""""""| INPUT: ____| | _______ | | | |____________| | | | | | | | | | | /"""""""""""\ | OUTPUT: _____/ \ | _____ \ | / \_________/LinearRamp constructor
Creates a
LinearRampobject.
LinearRamp(rate)
rate: the inital ramp rate in units per millisecond. Allowed data type:float.LinearRamp myRamp(0.1);LinearRamp operator
Returns the value of the output.
Data type:
long.long output = myRamp;update()
Updates the input of a
LinearRampobject.
myRamp.update(input)
myRamp: aLinearRampobject.input: the target value to ramp to. Allowed data typelong.Returns the value of the output. Data type:
long.
Reading the output is optional.