# Timer with long running code

**URL:** <https://community.particle.io/t/timer-with-long-running-code/42185>\
**Category:** Troubleshooting\
**Created:** [May 31, 2018, 6:13am UTC](https://community.particle.io/t/timer-with-long-running-code/42185 "2018-05-31T06:13:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![brian22](https://avatars.discourse-cdn.com/v4/letter/b/f08c70/32.png) [@brian22](https://community.particle.io/u/brian22)\
**Post date:** [May 31, 2018, 6:13am UTC](https://community.particle.io/t/timer-with-long-running-code/42185/1 "2018-05-31T06:13:59Z")

</div>

Is there a timer lib for running long, blocking operations? I have this code repeated several times:

```
bool flag;
void setFlag() {
  flag = true;
}
Timer timer(5000L, setFlag);
timer.start();

void loop() {
  if (flag) {
    flag = false;
    // time consuming operation
  }
}

```

I don’t need precise timing.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [May 31, 2018, 11:44am UTC](https://community.particle.io/t/timer-with-long-running-code/42185/2 "2018-05-31T11:44:55Z")

</div>

What kind of long, blocking operations are you thinking of?  
Can these not be made non-blocking?

---

<div class="post-metadata">

**Author:** ![brian22](https://avatars.discourse-cdn.com/v4/letter/b/f08c70/32.png) [@brian22](https://community.particle.io/u/brian22)\
**Post date:** [June 1, 2018, 4:47am UTC](https://community.particle.io/t/timer-with-long-running-code/42185/3 "2018-06-01T04:47:18Z")

</div>

Publish to MQTT, measure ultrasonic sensors

---

<div class="post-metadata">

**Author:** ![brian22](https://avatars.discourse-cdn.com/v4/letter/b/f08c70/32.png) [@brian22](https://community.particle.io/u/brian22)\
**Post date:** [June 1, 2018, 5:30am UTC](https://community.particle.io/t/timer-with-long-running-code/42185/4 "2018-06-01T05:30:19Z")

</div>

I tried wrapping the repetitive code in a class but get an error constructing the Particle timer:

```
# non_blocking_timer.h
# -------------------------------
#ifndef __NON_BLOCKING_TIMER__
#define __NON_BLOCKING_TIMER__

#include "application.h"

class NonBlockingTimer {

  typedef std::function<void(void)> timer_callback_fn;

  public:
    NonBlockingTimer(unsigned int period, timer_callback_fn callback);
    void start();
    void loop();

  private:
    void trigger();
    timer_callback_fn callback;
    Timer timer;
    bool triggered;
};

#endif // __NON_BLOCKING_TIMER__

# non_blocking_timer.cpp
# -------------------------------
#include "non_blocking_timer.h"
#include "application.h"

NonBlockingTimer::NonBlockingTimer(unsigned int period, timer_callback_fn callback)
  : callback(callback), timer(new Timer(period, &NonBlockingTimer::trigger, this)) // no matching function call to Timer::Timer()
{
}

void NonBlockingTimer::start()
{
  timer.start();
}

void NonBlockingTimer::loop()
{
  if (triggered) {
    triggered = false;
    callback();
  }
}

void NonBlockingTimer::trigger()
{
  triggered = true;
}

```

compiler error:

```auto
  no matching function call to Timer::Timer(unsigned int&, void (NonBlockingTimer::*)(), ...`

```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [June 1, 2018, 7:50am UTC](https://community.particle.io/t/timer-with-long-running-code/42185/5 "2018-06-01T07:50:38Z")

</div>

Usually with that error message also come some _note_ messages that reveal what the compiler actually expects for the closes overload of a call.  
In your case this

```nohighlight
x.ino: In constructor 'NonBlockingTimer::NonBlockingTimer(unsigned int, NonBlockingTimer::timer_callback_fn)':
x.ino:20:81: error: no matching function for call to 'Timer::Timer(unsigned int&, void (NonBlockingTimer::*)(), NonBlockingTimer*)'
x.ino:20:81: note: candidates are:
In file included from ./inc/application.h:59:0,
                 from src/x.cpp:1:
../wiring/inc/spark_wiring_timer.h:38:5: note: template<class T> Timer::Timer(unsigned int, void (T::*)(), T&, bool)
     Timer(unsigned period, void (T::*handler)(), T& instance, bool one_shot=false) : Timer(period, std::bind(handler, &instance), one_shot)
     ^
../wiring/inc/spark_wiring_timer.h:38:5: note: template argument deduction/substitution failed:
x.ino:20:81: note: deduced conflicting types for parameter 'T' ('NonBlockingTimer' and 'NonBlockingTimer*')
In file included from ./inc/application.h:59:0,
                 from src/x.cpp:1:
../wiring/inc/spark_wiring_timer.h:33:5: note: Timer::Timer(unsigned int, Timer::timer_callback_fn, bool)
     Timer(unsigned period, timer_callback_fn callback_, bool one_shot=false) : running(false), handle(nullptr), callback(std::move(callback_)) {
     ^
../wiring/inc/spark_wiring_timer.h:33:5: note: no known conversion for argument 2 from 'void (NonBlockingTimer::*)()' to 'Timer::timer_callback_fn {aka std::function<void()>}'
../wiring/inc/spark_wiring_timer.h:27:7: note: Timer::Timer(const Timer&)
 class Timer
       ^
../wiring/inc/spark_wiring_timer.h:27:7: note: candidate expects 1 argument, 3 provided

```

To interpret these notes you also need to know that `this` is a pointer to the object, not the object itself.

Also `new` returns a _pointer_ to the object, but you try to assign it to a object variable. Either make your `timer` a `Timer* timer` or dereference the resulting object pointer and assign to a reference `Timer &timer`.

---

<div class="post-metadata">

**Author:** ![brian22](https://avatars.discourse-cdn.com/v4/letter/b/f08c70/32.png) [@brian22](https://community.particle.io/u/brian22)\
**Post date:** [June 14, 2018, 6:13am UTC](https://community.particle.io/t/timer-with-long-running-code/42185/6 "2018-06-14T06:13:14Z")

</div>

Awesome, thanks for the detailed reply @ScruffR!

For future readers, my working solution in [non\_blocking\_timer.h](https://github.com/brianlow/garage/blob/master/src/non_blocking_timer.h) and [non\_blocking\_timer.cpp](https://github.com/brianlow/garage/blob/master/src/non_blocking_timer.cpp)
