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 bothOnDelay
andOffDelay
; 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
.Edge
is 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
Timer
object acts like a read-onlyunsigned long
that always counts up in milliseconds. It can be reset to 0 using thereset()
method.The value of the
Timer
is prevented from overflowing; once the timer reaches the highest value anunsigned long
can 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 }
Timer
is utilized in theOnDelay
,OffDelay
,Debounce
, andLinearRamp
classes.
OnDelay
INPUT: ___/""""""""""""""""""\___ | OUTPUT: ___|_________/""""""""\___ | | |<-DELAY->|
OnDelay constructor
Creates an
OnDelay
object.
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
OnDelay
object.
myOnDelay.update(input)
myOnDelay
: anOnDelay
object.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
OffDelay
object.
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
OffDelay
object.
myOffDelay.update(input)
myOffDelay
: anOffDelay
object.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
Debounce
object.
Debounce(delay)
delay
: the delay in milliseconds to wait before setting the outputtrue
and 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
Debounce
object.
myDebounce.update(input)
myDebounce
: aDebounce
object.input
: Allowed data typebool
.The value of the output. Data type:
bool
.
Reading the output is optional.setDelay()
Changes the delay of an
Debounce
object.
myDebounce.setDelay(delay)
myDebounce
: aDebounce
object.delay
: the delay in milliseconds to wait before setting the outputtrue
and 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
SquareWave
object.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 than1
and greater than0
. Allowed data type:float
.onPerid
: the period square wave istrue
/HIGH
in milliseconds. Allowed data type:unsigned long
.offPerid
: the period square wave isfalse
/LOW
in 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
Edge
object.
myEdge.update(input)
myEdge
: anEdge
object.input
: Allowed data typebool
.Nothing
rising()
Returns
true
when a rising edge is detected on the input.
myEdge.rising()
myEdge
: anEdge
object.Data type:
bool
.falling()
Returns
true
when a falling edge is detected on the input.
myEdge.falling()
myEdge
: anEdge
object.Data type:
bool
.
LinearRamp
|""""""""""""| INPUT: ____| | _______ | | | |____________| | | | | | | | | | | /"""""""""""\ | OUTPUT: _____/ \ | _____ \ | / \_________/
LinearRamp constructor
Creates a
LinearRamp
object.
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
LinearRamp
object.
myRamp.update(input)
myRamp
: aLinearRamp
object.input
: the target value to ramp to. Allowed data typelong
.Returns the value of the output. Data type:
long
.
Reading the output is optional.