Incorrect Timer period and stops after some time

arshad wrote on Monday, June 10, 2019:

Hello all,

Just starting with FreeRTOS and Arduino (Mega2560). I created a simple program to blink two LED,
Green every 5 sec
Blue turn on after 500ms

Behaviour i got:
-Blue turns on.
-Green blink but what seems like every 1 second
-After a minute Green is stuck at On and BLue starts blinking

help is appreciated

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

#define ONE_SHOT_TIMER_PERIOD       (pdMS_TO_TICKS(500))

int blueLedPin = 13, greenLedPin = 11;
const uint8_t *blueLed = (uint8_t *)&blueLedPin;
const uint8_t *greenLed = (uint8_t *)&greenLedPin;

TimerHandle_t xOneShotTimer;
TimerHandle_t xAutoReloadTimer;

BaseType_t xOneShotTimerStarted;
BaseType_t xAutoReloadTimerStarted;

void prvOneShotTimerCallback(TimerHandle_t xTimer);
void prvAutoReloadTimerCallback(TimerHandle_t xTimer);

void setup()
   Serial.println("Setup ");

   pinMode(blueLedPin, OUTPUT);
   pinMode(greenLedPin, OUTPUT);

   xOneShotTimer = xTimerCreate("One Shot",
                                       (void *)0,
   xAutoReloadTimer = xTimerCreate("Auto Reload",
                                           (void *)0,

   if ((xOneShotTimer != NULL) && (xAutoReloadTimer != NULL))
       xOneShotTimerStarted = xTimerStart(xOneShotTimer, 0);
       xAutoReloadTimerStarted = xTimerStart(xAutoReloadTimer, 0);

   while ((xOneShotTimerStarted != pdPASS) && (xAutoReloadTimerStarted != pdPASS)){}

void prvOneShotTimerCallback(TimerHandle_t xTimer)
   TickType_t xTimerNow;
   xTimerNow = xTaskGetTickCount();
   Serial.print("One-shot timer : ");
   digitalWrite(blueLedPin, digitalRead(blueLedPin) ^ 1);

void prvAutoReloadTimerCallback(TimerHandle_t xTimer)
   TickType_t xTimerNow;
   xTimerNow = xTaskGetTickCount();
   Serial.print("Auto- Reload timer : ");
   digitalWrite(greenLedPin, digitalRead(greenLedPin) ^ 1);

void loop(){}

rtel wrote on Monday, June 10, 2019:

I don’t know how the Arduino port works, or really how Arduino works,
but there is nothing obviously wrong with your code. Is the LED used by
any other code? Maybe the unexpected flashing pattern is controlled by
something else - an error handler for example.

Look though the following list for the normal options, such as ensuring
you don’t have stack overflow, you do have asserts defined, etc. In
this case it is the timer task stack that is of interest as the timer
callbacks both execute in the context of the timer task (which is also
why it is ok to access the serial port from both as they will never
interrupt each other).

arshad wrote on Tuesday, June 11, 2019:

Thanks Richard

Looks like i got some reading to do

arshad wrote on Tuesday, June 11, 2019:

follow up question.
Since Timer are executed by TimerTask, then will it be same if i have
two taks, one with 1/2 sec and one with 5 sec delay using vTaskDelay
instead of two timer?

rtel wrote on Tuesday, June 11, 2019:

Will what be the same?