• Stars
    star
    188
  • Rank 204,939 (Top 5 %)
  • Language
    C++
  • License
    MIT License
  • Created over 7 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Ticker library for Arduino

Arduino Ticker Library v4.x.x

Advice: for use with ESP boards and mbed based Arduino boards like Arduino Nano RP2040 Connect and Raspberry Pi Pico (using the official Arduino core) the TickTwo library https://github.com/sstaub/TickTwo is recommanded avoiding name conflicts.

The Arduino Ticker Library allows you to create easily Ticker callbacks, which can call a function in a predetermined interval. You can change the number of repeats of the callbacks, if repeats is 0 the ticker runs in endless mode. Works like a "thread", where a secondary function will run when necessary. The library use no interupts of the hardware timers and works with the micros() / millis() function. You are not (really) limited in the number of Tickers.

New in v4.0

  • added get interval function
  • added remaining function
  • added support for functional callbacks, only for ARM and ESP devices, e.g.
    "Examples/FunctionalARM/FunctionalARM.ino"

New in v3.1

  • added set interval function

New in v3.0

  • radical simplified API
  • generally you have to declare all settings in the constructor
  • deleted many set and get functions
  • if you need former functionality please use the version 2.1

New in v2.1

  • You can change the interval time to microseconds.
Ticker tickerObject(callbackFunction, 100, 0, MICROS_MICROS) // interval is now 100us
  • smaller improvements

New in v2.0

  • You can determine the number of repeats, instead of modes.
  • The internal resolution is now micros(), this works with intervals up to 70 minutes. For longer intervals you can change the resolution to millis().
Ticker tickerObject(callbackFunction, 1000, 0, MILLIS)
  • unified data types and smaller improvements

Installation

  1. "Download":https://github.com/sstaub/Ticker/archive/master.zip the Master branch from GitHub.
  2. Unzip and modify the folder name to "Ticker"
  3. Move the modified folder on your Library folder (On your Libraries folder inside Sketchbooks or Arduino software).

How to use

First, include the TimerObject to your project:

#include "Ticker.h"

Now, you can create a new object in setup():

Ticker tickerObject(callbackFunction, 1000); 
tickerObject.start(); //start the ticker.

In your loop(), add:

tickerObject.update(); //it will check the Ticker and if necessary, it will run the callback function.

IMPORTANT

If you use delay(), the Ticker will be ignored! You cannot use delay() command with the TimerObject. Instead of using delay, you can use the Ticker itself. For example, if you need that your loop run twice per second, just create a Ticker with 500 ms. It will have the same result that delay(500), but your code will be always state.

Example

Complete example. Here we created five timers, you can run it and test the result in the Serial monitor and the onboard LED.

#include "Ticker.h"

void printMessage();
void printCounter();
void printCountdown();
void blink();
void printCountUS();

bool ledState;
int counterUS;

Ticker timer1(printMessage, 0, 1); // once, immediately 
Ticker timer2(printCounter, 1000, 0, MILLIS); // internal resolution is milli seconds
Ticker timer3(printCountdown, 1000, 5); // 5 times, every second
Ticker timer4(blink, 500); // changing led every 500ms
Ticker timer5(printCountUS, 100, 0, MICROS_MICROS); // the interval time is 100us and the internal resolution is micro seconds


void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  delay(2000);
  timer1.start();
  timer2.start();
  timer3.start();
  timer4.start();
  timer5.start();
  }

void loop() {
  timer1.update();
  timer2.update();
  timer3.update();
  timer4.update();
  timer5.update();
  if (timer4.counter() == 20) timer4.interval(200);
  if (timer4.counter() == 80) timer4.interval(1000);
  }

void printCounter() {
  Serial.print("Counter ");
  Serial.println(timer2.counter());
  }

void printCountdown() {
  Serial.print("Countdown ");
  Serial.println(5 - timer3.counter());
  }

void printMessage() {
  Serial.println("Hello!");
  }

void blink() {
  digitalWrite(LED_BUILTIN, ledState);
  ledState = !ledState;
  }

void printCountUS() {
  counterUS++;  
  if (counterUS == 10000) {
    Serial.println("10000 * 100us");
    counterUS = 0;
    }
  }

Documentation

States

enum status_t {
  STOPPED,
  RUNNING,
  PAUSED
  };

Constructors

Ticker::Ticker(fptr callback, uint32_t timer, uint16_t repeats, interval_t mode)

Creates a Ticker object

  • callback for the function name you want to call
  • timer set the interval time in ms or us depending on mode
  • repeats set the number of repeats the callback should be executed, 0 is endless (default)
  • mode set the interval resolution to MILLIS, MICROS_MICROS or MICROS (default)

Example

Ticker timer(blink, 1000); // calls function blink() every second, internal resolution is micros, running endless
Ticker timer(blink, 1000, 5); // calls function blink() every second, internal resolution is micros, only 5 repeats
Ticker timer(blink, 1000, 0, MILLIS); // calls function blink() every second, internal resolution is millis, running endless
Ticker timer(blink, 1000, 0, MICROS_MICROS); // calls function blink() every 1000 microsecond, internal resolution is micros, running endless

Destructor

Ticker::~Ticker()

Destructor for Ticker object

Class Functions

Ticker Start

void Ticker::start()

Start the Ticker. Will count the interval from the moment that you start it. If it is paused, it will restart the Ticker.

Example

timer.start();

Ticker Resume

void Ticker::resume()

Resume the Ticker. If not started, it will start it. If paused, it will resume it. For example, in a Ticker of 5 seconds, if it was paused in 3 seconds, the resume in continue in 3 seconds. Start will set passed time to 0 and restart until get 5 seconds.

Example

timer.resume();

Ticker Pause

void Ticker::pause()

Pause the Ticker, so you can resume it.

Example

timer.pause();

Ticker Stop

void Ticker::stop()

Stop the Ticker.

Example

timer.stop();

Ticker Update

void Ticker::update()

Has to be called in the main while() loop, it will check the Ticker, and if necessary, will run the callback.

Example

while(1) {
  timer.update();
1.   }

Ticker set Interval Time

void Ticker::interval(uint32_t timer)

Changes the interval time of the Ticker. Depending on the mode it can millis or micro seconds.

  • timer set the interval time in ms or us depending on mode

Example

timer.interval(500); // new interval time

Ticker get Interval Time

uint32_t Ticker::interval()

Get the interval time of the Ticker. Depending on the mode it can millis or micro seconds.

Example

uint32_t intervalTime;
intervalTime = timer.interval(); // get the interval time

Ticker State

status_t Ticker::state()

Returns the state of the Ticker.

Example

status_t status;
status = timer.state();

Ticker Elapsed Time

uint32_t Ticker::elapsed()

Returns the time passed since the last tick in ms or us depending on mode.

Example

uint32_t elapse;
elapse = timer.elapsed();

Ticker Remaining Time

uint32_t Ticker::remaining()

Returns the remaining time to the next tick in ms or us depending on mode.

Example

uint32_t remain;
remain = timer.remaining();

Ticker Counter

uint32_t Ticker::counter()

Get the number of executed callbacks.

Example

uint32_t count;
count = timer.counter();

More Repositories

1

Ethernet3

Ethernet library for Arduino and Ethernetshield2 / WIZ550io / WIZ850io / USR-ES1 with Wiznet W5500 chip
C++
80
star
2

NTP

NTP library for Arduino framework
C++
46
star
3

TeensyID

Set of functions for different IDs of the Teensy: USB Serialnumber, Serialnumber, MAC-Address, ChipID
C++
33
star
4

TickTwo

Ticker library for ESP and mbed based Arduino boards
C++
31
star
5

Timer

Small library for measuring elapsed time between start and stop command
C++
29
star
6

gma3

Arduino library to control GrandMA3 lighting consoles using OSC
C++
28
star
7

lighthack

templates and examples for using OSC with ETC EOS family consoles
C++
26
star
8

eOS

An object orientated library for Arduino to control ETCs EOS Family Consoles with OSC over USB and Ethernet UDP
C++
24
star
9

eosstreamdeckplus

Stream Deck + configuration files for EOS consoles
22
star
10

NextionX2

Library for Nextion displays
C++
14
star
11

sACN

Send and receive sACN DMX packets following ANSI E1.31
C++
14
star
12

eOS-USB

An object orientated library for Arduino to control ETCs EOS Family Consoles with OSC over USB
C++
6
star
13

stm32EEPROM

A small mbed library for r/w of the internal EEPROM of the STM32L0xx and STM32L1xx microcontrollers.
C
5
star
14

SSD1803A_I2C

Arduino LCD library for EA DOGM204, DOGS164 and DOGS104 with SSD1803A controller over I2C
C++
5
star
15

gma3-Mbed

Mbed library for controlling GrandMA3 consoles with OSC using microcontrollers. License CC-BY-NC-SA 4.0
C++
5
star
16

eOS-ETH

An object orientated library for Arduino to control ETCs EOS Family Consoles with OSC over Ethernet
C++
4
star
17

mbedLCD

LCD HD44780 library for Mbed 6
C++
3
star
18

EADOG

mbed LCD library for DOG graphic LCDs from Electronic Assemnly
C
3
star
19

C12832A1Z

mbed LCD library for mbed application board and shield
C
2
star
20

LCDi2c

Arduino LCD library for HD44780 displays with I2C expander
C++
2
star
21

mbedLCDi2c

LCD HD44780 library with I2C adapter for Mbed 6
C++
2
star
22

OSC

Small library for creating OSC messages
C++
1
star
23

eOS3

Library to control ETC EOS software over OSC (USB/UDP/TCP)
C++
1
star
24

LCD

Arduino LCD library for HD44780 displays
C++
1
star
25

mbedTickers

A mbed library for creating Tickers which can call repeatly functions.
C++
1
star