Multiple Ticker objects crashing app

28 Apr 2011

Hi All,

I have an issue trying to use multiple Ticker objects, as in my application simply stops running at the point where I do the attach for the second Ticker? Looking around it seems this is the correct approach and there are several examples using multiple Ticker's so should work.

Time period for first one is 3600 seconds and the other at 900ms. One ticker is using attach_us() and the other attach()

Anyone seen this before?

Thanks, Serge

28 Apr 2011

Hmmmm,

Seems I cant set a Ticker for 3600 seconds, is the max limit mentions anywhere in the documentation which I might have missed?

Thanks, Serge

28 Apr 2011

Not sure but Tickers are possibly derived from the same source as Timers and that page states:-

Warning

Note that timers are based on 32-bit int microsecond counters, so can only time up to a maximum of 2^31-1 microseconds i.e. 30 minutes. They are designed for times between microseconds and seconds. For longer times, you should consider the time()/Real time clock.

28 Apr 2011

Thanks Andy,

This must be the case.

Serge

28 Apr 2011

You could try:-

#include "mbed.h"

Ticker t;

int t1, t2, tn;

void callback900(void) {
    ...
}

void callback3600(void) {
    ...
}

void myCallback(void) {
    if (t1++ == 900) {
        t1 = 0;
        callback900();
    }
    if (t2++ == (3600 * 1000)) {
        t2 = 0;
        callback3600();
    }
}

int main() {

    t1 = t2 = 0;
    t.attach_us(myCallback, 1000);

    ...
}

Maybe of use?

[edit: Btw, this is a little more deterministic since 36000000 is exactly divisible by 900. So at some future point you will know that t1 == 900 && t2 == 36000000 but you will know callback900 will also be called before callback3600, unless you swap them about that is. This is often better as you know for sure how things will pan out in the future. Using two tickers which are started at slightly different times and with their callbacks probably taking differing times to run will create a "beat" effect that may be unpredictable. Always try to remove things that may end up, at some point, being unpredictable if you can).

28 Apr 2011

Thanks, I thought of that ;-)

Serge