/*============================================================================================
This is basic FreeRTOS tutorial where a task is created, suspended, resumed and deleted
Using ESP32
*/
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include "driver/gpio.h"
const int PIN_L[] ={12, 13, 14, 25, 26, 27}; //These represent the pins to which the left LED are connected
const int PIN_R[] = {15, 16, 17, 18, 19, 21, 22, 23}; // These are the Right LED pins
int Size_Pin_L = (sizeof(PIN_L)/sizeof(PIN_L[0])); // get the number of element in the array
int Size_Pin_R = (sizeof(PIN_R)/sizeof(PIN_R[0])); // compute the number of element in this array
const TickType_t Delay = 50/ portTICK_PERIOD_MS; // This represent the delay time
int counter =0;
/*============ The left LEDs TAsk Handler==========================================*/
TaskHandle_t Left_LedTask =NULL;
// Fucntoin to be performed when task 1 is called
void Left_LED_Task(void *param){
for(;;){
for(int i = 0; i < Size_Pin_L; i++){
digitalWrite(PIN_L[i], HIGH);
vTaskDelay(Delay);
}
for(int i =0; i<Size_Pin_L; i++){
digitalWrite(PIN_L[i], LOW);
vTaskDelay(Delay);
}
}
}
/*============The Right LEDs TAsk Handler==========================================*/
TaskHandle_t Right_PinTask = NULL;
// Fucntoin to be performed when task 2 is called
void Right_Pin_Task(void *pvParameters ){
while (1){
counter ++;
for(int i = 0; i < Size_Pin_R; i++){
digitalWrite(PIN_R[i], HIGH);
vTaskDelay(Delay);
}
for (int i = 0; i<Size_Pin_R; i++){
digitalWrite(PIN_R[i], LOW);
vTaskDelay(Delay);
}
if(counter ==3){
vTaskSuspend(Right_PinTask);
Serial.println("LED_BUILTIN SUSPENDED");
}
if(counter == 8){
vTaskResume(Right_PinTask);
Serial.println("LED_BUILTIN RESUMED");
}
if (counter ==12){
vTaskDelete(Right_PinTask);
Serial.println("LED_BUILTIN Deleted");
}
}
}
//===============================The setup =================================
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32 with FreeRTOS");
for(int i = 0; i < Size_Pin_L; i++){
pinMode(PIN_L[i], OUTPUT);
}
for(int i = 0; i < Size_Pin_R; i++){
pinMode(PIN_R[i], OUTPUT);
}
pinMode(LED_BUILTIN, OUTPUT);
}
//==========================================The loop ====================================
void loop() {
// put your main code here, to run repeatedly:
xTaskCreatePinnedToCore(Left_LED_Task, "Left_LED_Task", 4000, NULL, 5, &Left_LedTask, 0);
xTaskCreatePinnedToCore(Right_Pin_Task, "Right_Pin_Task", 4000, NULL, 5, &Right_PinTask, 1);
}