Ticker library does not work with a function with parameters

I am trying to use the Ticker library and while the example works fine I am having an issue with getting it to work when I have a void function with parameters.
See code example below (which does not worK)

#include "Ticker.h"

void printMessage(int a, int b);

Ticker timer1(printMessage, 0, 1);


void setup() 
{
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  delay(2000);
  timer1.start();
  printMessage(1, 1);
}

void loop()
{
  timer1.update();
}


void printMessage(int a, int b) 
{
  if (a == 1 && b == 1)
  {
    Serial.println("Hello!");
  }
  else
  {
    Serial.println("Goodbye");
  }
}

and this the error I get at compile

Arduino: 1.8.15 (Windows 10), Board: "LOLIN(WEMOS) D1 R2 & mini, 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), 4MB (FS:2MB OTA:~1019KB), v2 Lower Memory, Disabled, None, Only Sketch, 921600"

Ticker:5:33: error: no matching function for call to 'Ticker::Ticker(void (&)(int, int), int, int)'

 Ticker timer1(printMessage, 0, 1);

                                 ^

C:\Users\proie\AppData\Local\Temp\arduino_modified_sketch_811228\Ticker.ino:5:33: note: candidates are:

In file included from C:\Users\proie\AppData\Local\Temp\arduino_modified_sketch_811228\Ticker.ino:1:0:

C:\Users\proie\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\libraries\Ticker\src/Ticker.h:32:5: note: Ticker::Ticker()

     Ticker();

     ^

C:\Users\proie\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\libraries\Ticker\src/Ticker.h:32:5: note:   candidate expects 0 arguments, 3 provided

C:\Users\proie\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\libraries\Ticker\src/Ticker.h:29:7: note: Ticker::Ticker(const Ticker&)

 class Ticker

       ^

C:\Users\proie\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\libraries\Ticker\src/Ticker.h:29:7: note:   candidate expects 1 argument, 3 provided

C:\Users\proie\AppData\Local\Temp\arduino_modified_sketch_811228\Ticker.ino: In function 'void setup()':

Ticker:13:10: error: 'class Ticker' has no member named 'start'

   timer1.start();

          ^

C:\Users\proie\AppData\Local\Temp\arduino_modified_sketch_811228\Ticker.ino: In function 'void loop()':

Ticker:19:10: error: 'class Ticker' has no member named 'update'

   timer1.update();

          ^

Multiple libraries were found for "Ticker.h"

 Used: C:\Users\proie\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\libraries\Ticker

 Not used: C:\Users\proie\Dropbox\ARDUINO\arduino 1.8.9\libraries\Ticker

 Not used: C:\Users\proie\Dropbox\ARDUINO\arduino 1.8.9\libraries\Ticker-master

exit status 1

no matching function for call to 'Ticker::Ticker(void (&)(int, int), int, int)'

Am I missing something as the examples given does not have a void function with parameters.

What type of Arduino board are you using? The library does not support callbacks with parameters for AVR-based boards. But, if it's ARM or ESP-based, you can specify the callback using std::function and bind an argument to it using std::bind. From Ticker.h:

#if defined(__arm__) || defined(ESP8266) || defined(ESP32)
#include <functional>
using fptr = std::function<void()>;
#else
typedef void (*fptr)();
#endif
void timerMessage()
{
  printMessage(0, 1);
}

Ticker timer1(timerMessage);

I am using an esp. Sorry I am not that advanced so i do not understand your answer

Thanks for that i will try that

Looking at the compile error message i noticed this

Used: C:\Users\proie\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\libraries\Ticker

 Not used: C:\Users\proie\Dropbox\ARDUINO\arduino 1.8.9\libraries\Ticker

 Not used: C:\Users\proie\Dropbox\ARDUINO\arduino 1.8.9\libraries\Ticker-master

Does this mean the ticker library comes with the esp package so there is no need to download and install in my library?

The ESP8266? Looks like it. Especially if your sketch compiled without error.

It probably means you have the library installed in too many places. You're also using an old version of the Arduino IDE (1.8.9) latest is v1.8.19.

Also, you haven't answered:

EDIT:
It does indeed look like there's an ESP8266-specific version of Ticker (by a different author) in the ESP8266 core. It's API is incompatible with the one installed by the Arduino IDE's Library Manager.

I did in post 4

"ESP" could be ESP8266 or ESP32. They're very different. Sometimes it's also helpful to identify the specific board you're compiling for.

I am using ESP8266

I have now uninstalled the Ticker library and am using the ESP8266 in built Ticker library and I now see your point of binding the argument. Thanks

Great, post your code as others might find it useful.

Working code for reference

#include "Arduino.h"
#include "Ticker.h"

#define LED1 2
#define LED2 4
#define LED3 12
#define LED5 15


class ExampleClass {
public:
  ExampleClass(int pin, int duration)
    : _pin(pin), _duration(duration) {
    pinMode(_pin, OUTPUT);
    _myTicker.attach_ms(_duration, std::bind(&ExampleClass::classBlink, this));
  }
  ~ExampleClass(){};

  int _pin, _duration;
  Ticker _myTicker;

  void classBlink() {
    digitalWrite(_pin, !digitalRead(_pin));
  }
};

void staticBlink() {
  digitalWrite(LED2, !digitalRead(LED2));
}

void scheduledBlink() {
  digitalWrite(LED3, !digitalRead(LED2));
}

void parameterBlink(int p, bool a)
{
  if(p == 1 && a == true)
  {
    Serial.println("Hello");
  }
  else
  {
    Serial.println("Goodbye");
  }
}

Ticker staticTicker;
Ticker scheduledTicker;
Ticker parameterTicker;
Ticker lambdaTicker;

ExampleClass example(LED1, 100);


void setup() {
  Serial.begin(9600);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED5, OUTPUT);
  
  staticTicker.attach_ms(100, staticBlink);
  scheduledTicker.attach_ms_scheduled(100, scheduledBlink);
  parameterTicker.attach_ms(100, std::bind(parameterBlink, 0, true));

  lambdaTicker.attach_ms(100, []() {
    digitalWrite(LED5, !digitalRead(LED5));
  });
}

void loop() {}
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.