Arduino Forum

Development => Other Software Development => Topic started by: sstaub on Apr 10, 2017, 02:38 pm

Title: New Ticker (Timer) Library
Post by: sstaub on Apr 10, 2017, 02:38 pm
I made a new Ticker library based on the ArduinoTimerObject library, which uses the millis() function to call a function repeatedly. You can find it on github:
https://github.com/sstaub/Ticker (https://github.com/sstaub/Ticker)
Title: Re: New Ticker (Timer) Library
Post by: sstaub on May 06, 2017, 11:23 am
I made a new version v2 with some new features:
- internal resolution now micros()
- you can determine the number of repeats of the callback function
https://github.com/sstaub/Ticker
Title: Re: New Ticker (Timer) Library
Post by: rbright on Mar 30, 2018, 10:30 am
Stefan your library works great on a UNO etc and I've seen your V3 BUT have you considered an adaption to it would work with ESPs ???
Please refer to my other topic as follows:
https://forum.arduino.cc/index.php?topic=537511.0.
It is unfortunate that there are 2 published libraries by the name Ticker.h

I look forward to your response
Title: Re: New Ticker (Timer) Library
Post by: sstaub on Apr 06, 2018, 07:28 pm
Have you tried my library for ESP? I think it should work for every platform which use the Arduino framework.
Title: Re: New Ticker (Timer) Library
Post by: rbright on Apr 08, 2018, 01:46 pm
Thanks for the continual refinement of your library. Hopefully in time it will allow code to be universal for any board like UNO or ESPs.

Following is some feedback on what I have discovered on 2 different windows 10 PCs. Today I even upgraded the IDE on both PCs from 1.8.3 to the latest 1.8.5 to see if that would make a difference.

Now when I run your example using your version 3.1 it compiles and runs fine on a UNO BUT when I try it on a ESP01, which has a different processor internally, I get the following compile errors:

In the IDE I selected Tools/Board: Generic ESP8266 Module and using Example.ino

Line 12 error: no matching function for call to 'Ticker::Ticker(void (&) () , int, int)'
Ticker timer1(printmessage, 0, 1);
                                              ^

is this a problem in one of your 'h' or 'cpp' files


I noticed during the clean installation of IDE 1.8.5 that after I go into Preferences and set 'Additional Boards ...' to find esp8266.com that the competing Library Ticker for ESP loads and its example files are now available under Files/Examples/Examples for Generic ESP8266  

Look forward to your comments
   
Title: Re: New Ticker (Timer) Library
Post by: sstaub on May 01, 2018, 04:08 pm
Have you tried
Code: [Select]
Ticker timer1(printMessage, 0, 1);. I think there is typo in your code (upper case M in printMessage. The code should work on every board which use the Arduino framework.
Title: Re: New Ticker (Timer) Library
Post by: marioifix on Jan 02, 2019, 07:43 pm
hi i have problem with library ticker
i use arduino uno

#include <Ticker.h>  //Ticker Library

boolean ledOn = false;
volatile int buttonState = LOW;
volatile int invia = 0;
#define LED   13                      //Led
#define STATO 7                       //Stato Acceso/Spento
#define METRI 2                       //Metri emessi
#define TEMPO 1                       //Tempo
Ticker tic;

void setup()
{
  Serial.begin(9600);
  pinMode(STATO, INPUT);
  pinMode(LED, OUTPUT);
  pinMode(METRI, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(METRI), interruptmetri, RISING);
  tic.attach(0.5, changeState);
}

//Invio dello stato del dispositivo
void changeState()
{
    if (digitalRead(STATO) == HIGH) {
      Serial.println ("1");
    } else {
      Serial.println ("2");
    }
}

void interruptmetri()
{
  invia++;
}

void loop()
{
          //Ci sono metri da inviare
        if ( invia > 0 ) {
             Serial.println ("Metri Cambiare con il valore voluto");
             invia--;
        }

}
Title: Re: New Ticker (Timer) Library
Post by: kowalski on Jan 02, 2019, 09:35 pm
@marioifix Did you forget
Code: [Select]
tic.update() in the
Code: [Select]
loop()?
Title: Re: New Ticker (Timer) Library
Post by: JamesHardiman on Apr 25, 2019, 03:55 pm
When can you (and can't you) start a Ticker?


I'm trying to understand exactly when things happen, depending on when you start a Ticker, and when you update.  I have modified the sample code, and it isn't behaving as I expected.

Here's my updated code.  The key thing I'm experimenting with here is that I don't start all the timers at once, although I do do a timerX.update for all timers at the top of the void loop{}.  I start timer1 in the setup, but then get each timer's callback procedure to start the next timer.  Is this a valid thing to do?

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

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

void printCounter() {
 Serial.print("Timer 2:  Counter ");
 Serial.println(timer2.counter());
  timer3.start();
 }

void printCountdown() {
 Serial.print("Timer 3: Countdown: 5 times a second ");
 Serial.println(5 - timer3.counter());
   timer4.start();
 }

void printMessage() {
 Serial.println("Timer 1: printMessage: Hello!");
 timer2.start();
 }

void blink() {
 digitalWrite(LED_BUILTIN, ledState);
 ledState = !ledState;
 Serial.print("Timer 4: blink ");
 Serial.print(String(ledState));
 Serial.print(", Counter=");
 Serial.println(String(timer4.counter()));
 timer5.start();
 }

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

... and here's the serial monitor:

First observation: Timer 4 (blink) seems to run a number of times without its counter incrementing.  I think everything else is behaving properly, but I'd appreciate reassurance on this!  I'm trying to write a piece of code that is driving me demented, and I think Ticker might save my life if only I can understand it correctly.

Timer 1: printMessage: Hello!
Timer 2:  Counter 1
Timer 3: Countdown: 5 times a second 4
Timer 4: blink 1, Counter=1
Timer 3: Countdown: 5 times a second 3
Timer 4: blink 0, Counter=1
Timer 5: Count Us: 10000 * 100us
Timer 3: Countdown: 5 times a second 2
Timer 4: blink 1, Counter=1
Timer 5: Count Us: 10000 * 100us
Timer 3: Countdown: 5 times a second 1
Timer 4: blink 0, Counter=1
Timer 3: Countdown: 5 times a second 0
Timer 5: Count Us: 10000 * 100us
Timer 4: blink 1, Counter=1
Timer 4: blink 0, Counter=2
Timer 5: Count Us: 10000 * 100us
Timer 4: blink 1, Counter=3
Timer 4: blink 0, Counter=4
Timer 4: blink 1, Counter=5
Timer 5: Count Us: 10000 * 100us
Timer 4: blink 0, Counter=6
Timer 4: blink 1, Counter=7
Timer 5: Count Us: 10000 * 100us
Timer 4: blink 0, Counter=8
Timer 4: blink 1, Counter=9
Timer 4: blink 0, Counter=10
Timer 5: Count Us: 10000 * 100us
Timer 4: blink 1, Counter=11
Timer 4: blink 0, Counter=12
Timer 5: Count Us: 10000 * 100us
Timer 4: blink 1, Counter=13
Timer 4: blink 0, Counter=14
Timer 4: blink 1, Counter=15
Timer 5: Count Us: 10000 * 100us
Timer 4: blink 0, Counter=16
Timer 4: blink 1, Counter=17
Timer 5: Count Us: 10000 * 100us
Timer 4: blink 0, Counter=18
Timer 4: blink 1, Counter=19
Timer 5: Count Us: 10000 * 100us
Timer 4: blink 0, Counter=20
Slowing to 200
Slowing to 200
Slowing to 200
Slowing to 200
Slowing to 200
Title: Re: New Ticker (Timer) Library
Post by: JamesHardiman on Apr 25, 2019, 04:12 pm
When can you (and can't you) start a Ticker? ... Continued


... I think I may have just answered my own question.  I changed the line(s) that start the next Ticker to:

if (timer4.counter()==0) timer4.start();  // Only start the timer if it's not already running.

That seems to have solved the problem, although comments are still welcome!

Timer 1: printMessage: Hello!
Timer 2:  Counter 1
Timer 3: Countdown: 5 times a second 4
Timer 4: blink 1, Counter=1
Timer 3: Countdown: 5 times a second 3
Timer 4: blink 0, Counter=2
Timer 4: blink 1, Counter=3
Timer 5: Count Us: 10000 * 100us
Timer 3: Countdown: 5 times a second 2
Timer 4: blink 0, Counter=4
Timer 4: blink 1, Counter=5
Timer 5: Count Us: 10000 * 100us
Timer 3: Countdown: 5 times a second 1
Timer 4: blink 0, Counter=6
Timer 4: blink 1, Counter=7