Search code examples
c++arduinoesp32freertosarduino-esp32

FreeRTOS: Run other tasks (with lower priority) while delay


I want to run multiple tasks on the same core of my ESP32 via FreeRTOS. I'm using the Arduino IDE. As I understood, the task with lower priority is only run, when the task with higher priority is finished. But I want that if the high priority task delays itself, the low priority task is run until the delay is over. Then the high priority task should be continued.

I googled a bit and asked ChatGPT, but every website said another thing:

  • Use vTaskDelay() or vTaskDelayUntil() (Sources which say to use it: Arduino Forum, ChatGPT; Source which say to not use it: Arduino Forum
  • Use semaphores (Sources which say to use it: Arduino Forum, ChatGPT; Source which say to not use it: Arduino Forum
  • Use vTaskSuspend() and vTaskResume() (Source which say to use it: ChatGPT; Source which say to not use it: me)

I've tried nothing of that. What is the best and easiest way to do it?


Solution

  • vTaskDelay is the canonical way to delay task operation giving room for other tasks to run. Note that there is also taskYIELD(), which – for a short moment in time – transfers control to the scheduler to do its thing.