Free Rtos

I have created two led blinking tasks using free RTOS, priority is same for both the tasks.

But only one first task is running.

More details required

Board ?
OS ?
IDE version ?
Code ?
Libraries used ?
Schematic ?

I am using arduino UNO board.

code:

#include <Arduino_FreeRTOS.h>
#define FREERTOS_CONFIG_H

#define INCLUDE_vTaskDelay 1
#define configUSE_PREEMPTION 1

#define led1 13
#define led2 12
// define two tasks for Blink & AnalogRead
void TaskBlink1( void *pvParameters );
void TaskBlink2( void *pvParameters );
void vTaskDelay( TickType_t xTicksToDelay );

// the setup function runs once when you press reset or power the board
void setup() {

// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(led2,OUTPUT);
pinMode(led1,OUTPUT);

while (!Serial) {
; // wait for serial port to connect. Needed for native USB, on LEONARDO, MICRO, YUN, and other 32u4 based boards.
}

// Now set up two tasks to run independently.
xTaskCreate(
TaskBlink1
, (const portCHAR *)“Blink1” // A name just for humans
, 500 // This stack size can be checked & adjusted by reading the Stack Highwater
, NULL
, 1 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
, NULL );

xTaskCreate(
TaskBlink2
, (const portCHAR *) “Blink2”
, 500 // Stack size
, NULL
, 1 // Priority
, NULL );

// Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
}

void loop()
{

// Empty. Things are done in Tasks.
}

/--------------------------------------------------/
/---------------------- Tasks ---------------------/
/--------------------------------------------------/

void TaskBlink1(void *pvParameters) // This is a task.
{
(void) pvParameters;

/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.

Most Arduinos have an on-board LED you can control. On the UNO, LEONARDO, MEGA, and ZERO
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN takes care
of use the correct LED pin whatever is the board used.

The MICRO does not have a LED_BUILTIN available. For the MICRO board please substitute
the LED_BUILTIN definition with either LED_BUILTIN_RX or LED_BUILTIN_TX.
e.g. pinMode(LED_BUILTIN_RX, OUTPUT); etc.

If you want to know what pin the on-board LED is connected to on your Arduino model, check
the Technical Specs of your board at Arduino - Products

This example code is in the public domain.

modified 8 May 2014
by Scott Fitzgerald

modified 2 Sep 2016
by Arturo Guadalupi
*/

for (;:wink: // A Task shall never return or exit.
{
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
}
}

void TaskBlink2(void *pvParameters) // This is a task.
{
(void) pvParameters;

for (;:wink:
{
// read the input on analog pin 0:
//int sensorValue = analogRead(A0);
// print out the value you read:
//Serial.println(sensorValue);
// one tick delay (15ms) in between reads for stability
digitalWrite(led2,HIGH);
vTaskDelay(1);
digitalWrite(led2,LOW);

}
}

Please edit the post using code tags, which will remove the smiley faces; see step 7 on the how to use this forum.

I'm not familiar with the free RTOS, but I do question how anything else is supposed to happen when you have a line that says

for ( ; ; ) // A Task shall never return or exit.

In TaskBlink1. (same for TaskBlink2, but one thing at a time)

ritesh_sharma:
I have created two led blinking tasks using free RTOS, priority is same for both the tasks.

IMHO it would be much simpler to do that without freeRTOS. Just use millis() for timing as illustrated in Several Things at a Time.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

Looks to me like the problem is that Task2 doesn't delay between setting the LED pin LOW and setting it HIGH again. Won't that keep the pin HIGH 99.99% of the time?

The delay is not the problem, second led remains in off state throughout the duration.

ritesh_sharma:
The delay is not the problem, second led remains in off state throughout the duration.

What about your code in the second task -

      // one tick delay (15ms) in between reads for stability
  digitalWrite(led2,HIGH);
  vTaskDelay(1);
  digitalWrite(led2,LOW);

This will write a high to led2 then 1 ms later write a low. Compare this task's code with task1's code.

ritesh_sharma:
The delay is not the problem, second led remains in off state throughout the duration.

Maybe it’s a wiring problem with your LED on Pin 12. What happens if you change from:

#define led1 13
#define led2 12

To;

#define led1 12
#define led2 13

stowite:
What about your code in the second task -

      // one tick delay (15ms) in between reads for stability

digitalWrite(led2,HIGH);
 vTaskDelay(1);
 digitalWrite(led2,LOW);




This will write a high to led2 then 1 ms later write a low. Compare this task's code with task1's code.

It doesn't seem it is a problem. Yes, should be vTaskDelay( 1000 / portTICK_PERIOD_MS );, however the
portTICK_PERIOD_MS is 1000 so the vTaskDelay(1) means 1s.

Please correct me if I am wrong but even though you have a comment to start the scheduler you never actually start it.