Issue with multiple imp.wakeup calls

I am having issues with running multiple imp.wakeup calls within the same program.

The objective of my code is to use the imp to check the time and then oscillate a LED at different frequencies based on what time it is. By oscillate, I mean vary the intensity 0-100% at different frequencies using the PWM function of the IMP.

My LED oscillation function looks like this:
`function ledslow()
{
if(dc >= 1)
{
inc = 0;
}
if(dc <= 0.05)
{
inc = 1;
}
dc = inc ? dc + 0.01 : dc - 0.01;
hardware.pin9.write(dc);

imp.wakeup(0.04, ledslow);

}`

where imp.wakeup(0.04, ledslow) oscillates the LED at a slow speed. I have a similar function, ledfast(), with imp.wakeup(0.01, ledfast), to oscillate the LED at a faster speed. Both of these functions have been tested and work perfectly fine on their own.

I then have a main function, main(), that uses date() to check the hour of day, and based on that, determine which LED oscillation function to call. main() uses imp.wakeup(1, main) to check the time every second.

When I run the entire program, the LED does indeed oscillate, but at an ever increasing speed until it reaches a maximum frequency. I am not sure what the terminology is, but I believe the imp.wakeup calls are compounding each other.

Am I doing something wrong? Is there a better way to accomplish this goal? I have read on the forums that the imp should be able to handle multiple imp.wakeup calls.

I am very new to squirrel so would appreciate any help I can get. Thanks.

Ahh, got it. Code works flawlessly now. Hugo and Peter, you guys are great. Thank you very much.

When you call ledslow, or ledfast, each second – what stops the previous call to ledslow or ledfast from continuing to run? Each time you call imp.wakeup, it schedules a new call of the callback. If the callback schedules itself, that’s an infinite sequence of callbacks. If you then call it again yourself, that’s two infinite sequences of callbacks…

What you probably want, is just one callback, that checks a global variable to see whether it should be acting as ledfast or ledslow.

Peter

I understand what you are saying, but not quite sure how to implement? Can you provide further explanation and/or sample code?

Thank you very much for your help.

…as in:

`ledfast <- false;

function led() {
if (ledfast) {
…led fast code goes here
imp.wakeup(0.01, led);
} else {
…led slow code goes here
imp.wakeup(0.04, led);
}
}

// start the led loop running
led();

// now you can change ledfast to true or false and the behavior will change appropriately`

I was having this problem too and did not realize that the callbacks would schedule themselves and create multiple infinite chains of callbacks. Thank you for this insight.