Running Multiple task parallel

Hello guys,
I want to run 4 task parallel
i.e 1st task should run after every 5 sec.
2nd runs after 1st task but it lasts for 40 to 50 sec,
3rd will run every 5 sec

response of 1st and 2nd task is quick

13032020.zip (9.91 KB)

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

nishikanthingole:
I want to run 4 task parallel
i.e 1st task should run after every 5 sec.
2nd runs after 1st task but it lasts for 40 to 50 sec,
3rd will run every 5 sec

There's one missing :wink:

Sorry i forget to mention that i am using esp32.

what thing is missing?

And it's unclear (to me at any rate) what the relationship between tasks 1 and 2 is.

From time 0, task 1 starts at t=5 yes?

Then at t=5+ how long task 1 takes, task2 starts so task 2 completes at t= 5+ something + 40to50 seconds, somewhere around t=60, yes?

But meantime, task1 which runs every 5 seconds has started again .... and would / could run many times during task2's 40to50 seconds, so when does task 2 run again?

Thanks for replying so quick.

Consider there is a loop of 1 min
within that task 1 and task 3 should record 12 readings and task 2 record only ones

nishikanthingole:
what thing is missing?

You say there are 4 tasks but only talk of 3...

4th is button that turn on/off the device
runs throughout

nishikanthingole:
Thanks for replying so quick.

Consider there is a loop of 1 min
within that task 1 and task 3 should record 12 readings and task 2 record only ones

Still not sure then what it means that task 2 runs after task 1…

But is it this (and no idea about task 4 :wink: )

At t=0, task 1 and 3 start, and occur every 5 seconds, so t=10, 15, 20 etc
At t=5, that is after task 1 runs once, task 2 starts and runs for 40/50 seconds, so only once in each minute

ok you typed this while I was typing:

4th is button that turn on/off the device

What device? Just cut the power to Arduino or is the Arduino controlling some other device?

I think you would have more luck here with a circuit diagram and perhaps draw out a timeline.

In above post i attached my full code (except button operation i.e 4th task)
Yes other devices are connected via I2C i.e HR5 click, ENS210 and LSM303
main controller is ESP32.
in main .ino >> in loop >> 3 tasks are given

i go through this link but ideas are not get cleared

define.h|attachment (781 Bytes)

fun_def.cpp|attachment (26.4 KB)

Header.h|attachment (3.98 KB)

13032020.ino (5.33 KB)

study state machines (and what millis() can bring)
your events will be time passing and the button that turns the device on/off

The button function is that when i press it for 0.35 sec device gets turn on and when i press it for 1.5 sec it will get turn off
After turning on the device other task starts to work simultaneously

I'm certainly not going to try to wade through and understand all your code tabs. I was trying to help in a generic sense of how to get things going at the same time, but I still don't understand the relationship between the 3 or 4 tasks.

J-M-L suggested a state machine approach; try to provide a state diagram (or at least a time line as I suggested above) that shows how the tasks all link together in time.

Please check attachment for timing diagram

this is your image (learn how to post them here)

just read how to use millis() and use state variables to decide when to trigger what.

previously i was using this in my code but not working

Now trying from this link

https://techtutorialsx.com/2017/12/30/esp32-arduino-using-the-pthreads-library/ads-library/[/url]

Multi-threaded applications are more complicated to debug and thread syncing if necessary not always obvious. You also depend on freeRTOS for the scheduling.

if you know what you are doing, why not — they provide an interesting abstraction.

How does task 4 run to poll the switch turn it on when it's off so not running... I think I asked this before: does it just cut and restore the power to the Arduino like an actual power switch? If so it's not part of the coding surely?

Apart from that, does that whole thing repeat? So as long as it's not turned off, it cycles?

Your task 2 is only on for 7x5 = 35 not 40, so is that variable? You say task 2 starts "after first 5s when task 1 runs" which makes sense if that's from power up, but what about repeats. What's the "first 5s" then? Can you show two full cycles please?

also how complex are the tasks ? can they be split in few microseconds sub-segments that can be called repeatedly for progress or do they require heavy, resource hungry computation that is not easy to break down?

A button is used to turn the device ON/OFF
When device gets ON all mentioned task should run
2nd task is heart rate ans SpO2 measurement, it takes time
meanwhile task 1 i.e. temp and humidity measurement task, it records 12 values in 1 min.
3rd task is of acceleration, it also do the same as task 1,, it also records 12 values in 1 min.

After all recording, all data send to other esp32 via BLE

This is the flow of my code

When i run this flow one after other works fine.