How to use software timer within a class?

I need to use a software timer within a class but I cannot get the code to compile.

The should work _updateTimer = new Timer(4, &myClass::update, this); as it follows the same syntax as Particle.function("control", &myClass::control, this); which does compile.

I read some other posts on the forum about using lambda to solve this issue but it seems like it should be unneccessary, plus I could not get a lambda to compile.

How should I be instantiating and using a software timer within a class so that it calls a function: void myClass::update() {...}



If you called your class MyClass…
In your header file (.h), usually in Private.

Timer* timerObj;

In Class begin() function (it is my understanding this won’t work in you class constructor because you can’t guarantee in what order objects will be instantiated.

timerObj = new Timer(50, (void (*)())&myClass::update);

To start and stop your timer (any Timer Class member function actually)


It works, thanks.

Particle need to remove the need for this or document it somewhere as it’s really not clear.

Thanks for the heads up, we’ll look into adding similar class function support as exists with other callbacks in the API.


I have tried the syntax listed here, but I cannot get this to compile? I get;
no matching function for call to 'Timer::Timer(int, void (Foo::*)(), Foo&)

The code in the header file is;
Class Foo{
void onTimeout();
_Timer *myTimer;
Foo(int x, int y, int z);

and in the cpp;
Foo::Foo(int x, int y, int z){
myTimer = new Timer (x, (void (*)())&Foo::onTimeout);

I’m really trying to get my head around this but I am struggling with the class member functions! Any help would be appreciated!

Also, I have tried the code from this thread as well.
And both will not compile.

I am working on an electron project and incidentally I tried compiling for a photon and it worked???
Maybe calling member class functions is not supported on the electron?

You haven’t marked the class method static.
When you want to pass a method to a function that is ignorant of the object instances the method needs to be static to be resolved by the compiler.
To have non-static functions play well, the receiving function needs to learn about the instance pointer of the respective object too.

Hence there now is a dedicated function to set up an object method too

Hence try this in your .cpp

myTimer = new Timer (x, &Foo::onTimeout, *this);

Thanks @ScruffR
I gave that a crack but I still get an error;

The error seems to be the ‘this’ so maybe the timers are not allowed to be class members?
I have tried the code from the docs, and that works fine as a non class member but it doesn’t work as soon as it is a class member.with the ‘this’.

Yup, I forgot the deref asterisk :blush: (added above now)

Try this one

class Foo {
    Timer t1 = Timer(1000, &Foo::cb, *this);
    Timer t2;

    Foo() : t2(Timer(2000, &Foo::cb, *this)) { 

    void begin() {

    void cb() { 


Foo f;
void setup()

It compiles! :grinning:

You have no idea how long I have spent trying to get that to work! Legend :+1: