Skip to content

This is an Arduino library for managing event timing.

License

Notifications You must be signed in to change notification settings

CMB27/AutomationTimers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AutomationTimers

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 reset
  • OnDelay: 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 both OnDelay and OffDelay; 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.

Compatibility

This library should work on any microcontroller board. It relies on millis() and abs() from the Arduino API/Language, but has no other dependencies.

Examples

Method

update()

Description

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 of loop().

Syntax

AutomationTimers.update()

Example

#include <AutomationTimers.h>

void setup() {
  // run setup stuff here
}

void loop() {
  AutomationTimers.update();
  // run other loop stuff here
}

Classes

Timer

Description

A Timer object acts like a read-only unsigned long that always counts up in milliseconds. It can be reset to 0 using the reset() method.

The value of the Timer is prevented from overflowing; once the timer reaches the highest value an unsigned long can hold, it will stay there until reset.

Example

#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);
}

Methods

Timer constructor

Description

Creates a Timer object.

Example

Timer myTimer;
Timer operator

Description

Returns the value of the timer in milliseconds.

Returns

Data type: unsigned long.

Example

if (myTimer >= 2000) {
  // do something
}
reset()

Description

Resets the timer to 0.

Example

if (myTimer >= 2000) {
  myTimer.reset();
  // do something else
}

Note

Timer is utilized in the OnDelay, OffDelay, Debounce, and LinearRamp classes.

OnDelay

Description

INPUT:  ___/""""""""""""""""""\___
           |
OUTPUT: ___|_________/""""""""\___
           |         |
           |<-DELAY->|

Methods

OnDelay constructor

Description

Creates an OnDelay object.

Syntax

OnDelay(delay)

Parameter

delay: the delay in milliseconds to wait before setting the output true. Allowed data type: unsigned long.

Example

OnDelay myOnDelay(1000);
OnDelay operator

Description

Returns the value of the output.

Returns

Data type: bool.

Example

if (myOnDelay) {
  // do something
}
update()

Description

Updates the input of an OnDelay object.

Syntax

myOnDelay.update(input)

Parameters

  • myOnDelay: an OnDelay object.
  • input: Allowed data type bool.

Returns

The value of the output. Data type: bool.
Reading the output is optional.

setDelay()

Description

Changes the delay of an OnDelay object.

Syntax

myOnDelay.setDelay(delay)

Parameters

  • myOnDelay: an OnDelay object.
  • delay: the delay in milliseconds to wait before setting the output true. Allowed data type: unsigned long.
OffDelay

Description

INPUT:  ___/""""""""\_____________
                    |
OUTPUT: ___/""""""""|"""""""""\___
                    |         |
                    |<-DELAY->|

Methods

OffDelay constructor

Description

Creates an OffDelay object.

Syntax

OffDelay(delay)

Parameter

delay: the delay in milliseconds to wait before setting the output false. Allowed data type: unsigned long.

Example

OffDelay myOffDelay(1000);
OffDelay operator

Description

Returns the value of the output.

Returns

Data type: bool.

Example

if (myOffDelay == false) {
  // do something
}
update()

Description

Updates the input of an OffDelay object.

Syntax

myOffDelay.update(input)

Parameter

  • myOffDelay: an OffDelay object.
  • input: Allowed data type bool.

Returns

The value of the output. Data type: bool.
Reading the output is optional.

setDelay()

Description

Changes the delay of an OffDelay object.

Syntax

myOffDelay.setDelay(delay)

Parameters

  • myOffDelay: an OffDelay object.
  • delay: the delay in milliseconds to wait before setting the output false. Allowed data type: unsigned long.
Debounce

Description

INPUT:  ___/""""""""""""""""""\_____________
           |                  |
OUTPUT: ___|_________/""""""""|"""""""""\___
           |         |        |         |
           |<-DELAY->|        |<-DELAY->|

Methods

Debounce constructor

Description

Creates a Debounce object.

Syntax

Debounce(delay)

Parameter

delay: the delay in milliseconds to wait before setting the output true and the delay to wait before setting the output false. Allowed data type: unsigned long.

Example

Debounce myDebounce(1000);
Debounce operator

Description

Returns the value of the output.

Returns

Data type: bool.

Example

if (myDebounce) {
  // do something
}
update()

Description

Updates the input of an Debounce object.

Syntax

myDebounce.update(input)

Parameters

  • myDebounce: a Debounce object.
  • input: Allowed data type bool.

Returns

The value of the output. Data type: bool.
Reading the output is optional.

setDelay()

Description

Changes the delay of an Debounce object.

Syntax

myDebounce.setDelay(delay)

Parameters

  • myDebounce: a Debounce object.
  • delay: the delay in milliseconds to wait before setting the output true and the delay to wait before setting the output false. 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 than 1 and greater than 0. Allowed data type: float.
  • onPerid: the period square wave is true/HIGH in milliseconds. Allowed data type: unsigned long.
  • offPerid: the period square wave is false/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

Description

INPUT:   ___/""""""""""\____
RISING:  ___/\______________
FALLING: ______________/\___
CHANGE:  ___/\_________/\___

Methods

Edge constructor

Description

Creates a Edge object.

Example

Edge myEdge;
Edge operator

Description

Returns the value of the input.

Returns

Data type: bool.

Example

bool input = myEdge;
update()

Description

Updates the input of an Edge object.

Syntax

myEdge.update(input)

Parameters

  • myEdge: an Edge object.
  • input: Allowed data type bool.

Returns

Nothing

rising()

Description

Returns true when a rising edge is detected on the input.

Syntax

myEdge.rising()

Parameter

myEdge: an Edge object.

Returns

Data type: bool.

falling()

Description

Returns true when a falling edge is detected on the input.

Syntax

myEdge.falling()

Parameter

myEdge: an Edge object.

Returns

Data type: bool.

change()

Description

Returns true when a change is detected on the input.

Syntax

myEdge.change()

Parameter

myEdge: an Edge object.

Returns

Data type: bool.

LinearRamp

Description

            |""""""""""""|
INPUT:  ____|            |             _______
                         |            |
            |            |____________|
            |
            |            |            |
            |            |            |
            |                         |
            | /"""""""""""\           |
OUTPUT: _____/             \          |  _____
                            \         | /
                             \_________/

Methods

LinearRamp constructor

Description

Creates a LinearRamp object.

Syntax

LinearRamp(rate)

Parameter

rate: the inital ramp rate in units per millisecond. Allowed data type: float.

Example

LinearRamp myRamp(0.1);
LinearRamp operator

Description

Returns the value of the output.

Returns

Data type: long.

Example

long output = myRamp;
update()

Description

Updates the input of a LinearRamp object.

Syntax

myRamp.update(input)

Parameters

  • myRamp: a LinearRamp object.
  • input: the target value to ramp to. Allowed data type long.

Returns

Returns the value of the output. Data type: long.
Reading the output is optional.

setRate()

Description

sets the ramp rate in units per millisecond of a LinearRamp object.

Syntax

myRamp.setRate(rate)

Parameters

  • myRamp: a LinearRamp object.
  • rate: the ramp rate. Allowed data type: float.