How to pass variables to custom callback functions I want to use the Ticker library of the ESP8266 Arduino core to (asynchronously) delay the switch of a pin to a desired state like below. I am not sure about the "here is" function definitions and I am having a hard time to find some documentation. Please have a look at what I tried so far:

#include &lt;Ticker.h&gt;

Ticker change_pin;

...

    uint8_t desired_state = 1;
    ...
    change_pin.attach_ms(100, [](uint8_t state_to_change_to){
        if( digitalRead(PIN) != state_to_change_to ) {
            digitalWrite(PIN, state_to_change_to);
        }
        else {
            change_pin.detach();
        }
    }, desired_state);


But the compiler's yelling:

espIKEA:145: error: no matching function for call to 'Ticker::attach_ms(int, toogle_light(uint8_t)::__lambda0, uint8_t&amp;)'

   }, des_state);

               ^

...

C:\Users\USER\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\Ticker/Ticker.h:45:7: note:   candidate expects 2 arguments, 3 provided

C:\Users\USER\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\Ticker/Ticker.h:62:7: note: template&lt;class TArg&gt; void Ticker::attach_ms(uint32_t, void (*)(TArg), TArg)

  void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)

       ^

C:\Users\USER\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\Ticker/Ticker.h:62:7: note:   template argument deduction/substitution failed:

C:\Users\USER\Documents\Arduino\Scratch\pintest\pintest.ino:145:15: note:   mismatched types 'void (*)(TArg)' and 'toogle_light(u8)::__lambda0'

   }, des_state);

               ^


So, I am having too few knowledge to understand what to do about it. I really would like to avoid having a separate function for the callback body.

PS: Important source file can be found here: Ticker.h
The function you are trying to call is a template function:

template&lt;typename TArg&gt;
void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
{
    ....
}


So, you need to tell it what type of parameter you are passing:

change_pin.attach_ms&lt;uint8_t&gt;(100, [](uint8_t state_to_change_to) {
//                     ^- tell the compiler about the parameter
    ....
}, desired_state);




To pass more than one parameter: Since the library only allows one parameter, I would create a struct and pass a pointer to it.

struct callback_parameter {
    int i;
    char *p;
};


Then create one of these structs and pass the address of it. You just have to make sure the struct is valid the entire time the timer is running. So either use new, static, or make it global.

callback_parameter param; // global variable

...

change_pin.attach_ms&lt;callback_parameter *&gt;(100, [](callback_parameter * state_to_change_to) {
    ....
}, &amp;param);