vTaskStartScheduler() without checking xTimerStart()

Hi. I want to start and stop a timer inside a task function. So that I use vTaskStartScheduler() in setup(). But it seems that the time hasn’t started yet.

#include <Arduino_FreeRTOS.h>
#include <timers.h>

int enbA = 3;
int in1 = 5;
int in2 = 6;
int in3 = 9;
int in4 = 10;
int enbB = 11;
int key=0;
TaskHandle_t CheckTask_t;
TimerHandle_t myTimer;
BaseType_t xTimerStarted;
BaseType_t xTimerStopped;

void setup() {
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(enbA, OUTPUT);
  pinMode(enbB, OUTPUT);
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  digitalWrite(enbA, LOW);
  digitalWrite(enbB, LOW);
  xTaskCreate(CheckTask, "Check", 128, NULL, 2, &CheckTask_t);
  myTimer = xTimerCreate("MyTimer",
  if (myTimer != NULL)

void loop() {}

void CheckTask(void *p)
  for (;;)
    if (key=0)
      xTimerStarted = xTimerStart(myTimer, 0);
      xTimerStopped = xTimerStop(myTimer, 0);

static void prvTimerCallback(TimerHandle_t myTimer)

void dunglai() {
  analogWrite(enbA, 0);
  analogWrite(enbB, 0);
  digitalWrite(in1, 0);
  digitalWrite(in2, 0);
  digitalWrite(in3, 0);
  digitalWrite(in4, 0);

It looks like you are calling xTimerStart() continuously in a loop. That will fill the timer command queue and may cause the timer task to spend all its time draining the queue.

Does the timer callback execute once? Or not at all?

What is configTIMER_TASK_PRIORITY set to?

I see. I didn’t set configTIMER_TASK_PRIORITY, but the callback function is still called successfully.

Please, let’s take a look at here. sketch.ino - Wokwi Arduino and ESP32 Simulator
I set daemon task priority to 1, but callback function still executes.

Looking at that code, as the timer is created as a one-shot timer I would expect the callback to execute once.

Yes. But I mean, even I set configTIMER_TASK_PRIORITY to 1, less than 2 (CheckTask priority), the callback function is still executed.

I recommend taking a look at the FreeRTOS book, which is a free download from here: Free RTOS Book and Reference Manual. It is a bit (a lot) out of date, but still very relevant.

I created two tasks, one will start the timer. sketch.ino - Wokwi Arduino and ESP32 Simulator. When running, I realized that the timer only started once. :frowning:

That code is really convoluted. Anytime I see tasks being suspended and resumed my immediate thought is there is a misunderstanding - I really suggest you read the book i already posted a link to and get a development environment that has a debugger.

What are you trying to achieve? If I know that I may be able to suggest an answer.

One thing I can see wrong is the #define that sets the priority of the timer task at the top of the C file. Any #defines that configure the kernel have to be in the configuration header file which normally is FreeRTOSConfig.h, but I don’t know about Arduino.

I want to alternate the two tasks ExecuteTaskCheckTask. I used semaphore before but it didn’t work, so I change to use suspend and resume.

What do you want to achieve by alternating? Can you describe the problem that you are trying to solve?