# need help to write variable delay function for different event

#### bug13

Joined Feb 13, 2012
1,863
Hi guys

In my embedded system, I usually have a timer to keep time, and I usually have a many different tasks to run in different interval in the main loop. That's how I usually do it:
Code:
while(1)
{
time_now = get_now();

if (( time_now - time_xyz_task ) > time_1s )

if (( time_now - time_abc_taks ) > time_100ms )
do_taks_abc();

}
It's OK to only have a few task, what if I have 20 different task need to run at different interval, is that a way to write a function like this:
Code:
  schedule( run_every_1s, some_task_1());
schedule( run_every_ns, some_task_n());
Thanks guys

#### dannyf

Joined Sep 13, 2015
2,197
It is certainly doable. How to do it will depend on the kind of compromises or complexity you are willing to take.

The simplest would be to keep a system tick that advances periodically. At its advancement, you check for scheduled tasks against event tick. Once an event is due to execute, set a flag for its execution and advance the event tick.

You can encompass all of those parameters in a strict so you can pass that strict to an update function that will do it all for you .

Or you can run a rtos.

BTW, your code isn't overflow safe.

#### bug13

Joined Feb 13, 2012
1,863
BTW, your code isn't overflow safe.
You mean my code in my first post? Can you tell me more? I have no idea my code is not overflow safe.thanks

#### ErnieM

Joined Apr 24, 2011
8,053
I believe he means you could overflow your call stack with so many things going on at the same time. It is an understated problem with small processors with limited memory that can lead to troublesome hard to reproduce almost random errors and crashes.

What I do see in your first cut is you complete one task before beginning another, which keeps the stack as small as possible.

What I don't see is any sort of prioritizing the routines. If the 1 sec interval is running do you stop it for the one running every millisecond? If you do... Up pops the overflow problem.

Writing direct code for multiple events may be cleaner than some general routine where events are registered and certainly takes less ram for more space for the stack.

A general routine is possible as you can create a structure to save the time interval and a function pointer, and call the pointer when the time is expired.

#### bug13

Joined Feb 13, 2012
1,863
I believe he means you could overflow your call stack with so many things going on at the same time. It is an understated problem with small processors with limited memory that can lead to troublesome hard to reproduce almost random errors and crashes.

What I do see in your first cut is you complete one task before beginning another, which keeps the stack as small as possible.

What I don't see is any sort of prioritizing the routines. If the 1 sec interval is running do you stop it for the one running every millisecond? If you do... Up pops the overflow problem.

Writing direct code for multiple events may be cleaner than some general routine where events are registered and certainly takes less ram for more space for the stack.

A general routine is possible as you can create a structure to save the time interval and a function pointer, and call the pointer when the time is expired.