简体   繁体   中英

"Reuse" a function multiple times at the same time? How? (Arduino, C++)

So I have a timer function:

void Timer(unsigned long milliseconds, bool &toggle) {
    static unsigned long timePassed;
    static unsigned long timerBegin;
    if (timePassed <= milliseconds) {
        timePassed = millis() - timerBegin;
    }
    else {
        timePassed = 0;
        timerBegin = millis();
        toggle = !toggle;
    }
    return toggle;
}

and the Timer is activated by creating a bool and assigning it to the timer, like this:

static bool whatever = false;

Timer(1000, whatever);

The whatever bool then toggles every 1000ms. But when I run the Timer function multiple Times at the same time, only the first Timer is working, the other bools just stay false and don't change. How can I fix this? Is that even fixable?

You should read about recursive functions and Stack and Heap. You are getting this Error because when running a function it will execute until the next function is called. The first function is then put to a halt until the next function you called is executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM