RTOS Question

hey there, I'm totally new to RTOS, and here is my question.
I used below code which is very simple code for creating and performing 3 Tasks. I wondered what will happen if I just remove the while loop in Task1 or2, and I thought that maybe these Tasks will be executed only once but what happened was that they kept working and instead, Task3 just shows "1" in Serial Monitor instead of showing the counting...
why is that?

#include <Arduino_FreeRTOS.h>

//TaskHandle_t xTaskHandle;

void TaskBlink1(void *pvParameters);
void TaskBlink2(void *pvParameters);
void Taskprint(void *pvParameters);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  xTaskCreate(TaskBlink1,"Task1",128,NULL,1,NULL);
  xTaskCreate(TaskBlink2,"Task2",128,NULL,1,NULL);
  //xTaskCreate(TaskBlink2,"Task2",128,NULL,1,&xTaskHandle);
  xTaskCreate(Taskprint,"Task3",128,NULL,1,NULL);
  vTaskStartScheduler();
}

void loop() {
  // put your main code here, to run repeatedly:

}

void TaskBlink1(void *pvParameters)  {
  pinMode(8, OUTPUT);
  while(1)
  {
    Serial.println("Task1");
    digitalWrite(8, HIGH);   
    vTaskDelay( 200 / portTICK_PERIOD_MS ); 
    digitalWrite(8, LOW);    
    vTaskDelay( 200 / portTICK_PERIOD_MS ); 
 }
}
void TaskBlink2(void *pvParameters)  
{
  
  //vTaskDelete(NULL); //for Deleting Task2 inside Task2 itself and thus the second LED won't blink anymore and just "Task2" will appeare and then it will be deleted 
  pinMode(7, OUTPUT);
  while(1)
  {
    Serial.println("Task2");
    digitalWrite(7, HIGH);   
    vTaskDelay( 300 / portTICK_PERIOD_MS ); 
    digitalWrite(7, LOW);   
    vTaskDelay( 300 / portTICK_PERIOD_MS ); 
  }
}
void Taskprint(void *pvParameters)  {
  int counter = 0;
  while(1)
  {
    counter++;
    Serial.println(counter); 
    vTaskDelay(500 / portTICK_PERIOD_MS);    
  }
}

try

  static int counter = 0;

I thought that maybe these Tasks will be executed only once but what happened was that they kept working

That's what a scheduler does.

alaa72:
I wondered what will happen if I just remove the while loop in Task1 or2,

You should never allow a FreeRTOS task's function to reach its end and return.

Dear gfvalvo

You should never allow a FreeRTOS task's function to reach its end and return.

why is that?

alaa72:
Dear gfvalvo why is that?

Because that's the way FreeRTOS is designed to work. If you allow the function to return, the behavior will be undefined and indeterminate.

try
Code: [Select]

 static int counter = 0;

I tried this and this time it counted to 2, and again started from 1, and then 2,etc