#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
// Priority value of the two task
// Can and should use BaseType for efficiency and portability
const int LED_TASK_PRIORITY = 2;
const int BUTTON_TASK_PRIORITY = 1;
// Pointer that refer to the two task
TaskHandle_t xLedTaskHandle = NULL;
TaskHandle_t xButtonTaskHandle = NULL;
// Designated Pin
const int BUTTON_PIN = 2;
const int LED_PIN = 5;
// Function to be executed in the two task
static void vLedTask (void* parameter)
{
// "Eternal" task
while(1)
{
// 20 Percent "Duty" Cycle, 500ms Period
digitalWrite(LED_PIN, HIGH);
vTaskDelay(100 / portTICK_PERIOD_MS);
digitalWrite(LED_PIN, LOW);
vTaskDelay(400 / portTICK_PERIOD_MS);
Serial.print("IodineGamingHD \n");
}
}
static void vButtonTask (void* parameter)
{
int ButtonState = 0; // Pushed=1, Relaxed=0
int isLEDRunning = 1;
// "Etenal" task
while(1)
{
ButtonState = digitalRead(BUTTON_PIN);
if(ButtonState == 1)
{
vTaskSuspend(xLedTaskHandle);
Serial.println("PotassiumGamingHD");
isLEDRunning = 0;
}
else
{
// Sometimes FreeRTOS doesnt know that a function does not
// need to be ran anymore.
// We need to specify the decision making ourselves with if control flow.
if(isLEDRunning == 0)
{
vTaskResume(xLedTaskHandle);
isLEDRunning = 1;
}
}
}
}
void setup()
{
// 9600 baud rate, as to spec
Serial.begin(9600);
// Pin Initialization
pinMode(BUTTON_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
// Task initialization
Serial.print(xTaskCreate(vLedTask, // Task function
"vLedTask", // Task name,
1024, // 1 KiloBit
NULL, // Parameter
LED_TASK_PRIORITY, // Task priority scale
&xLedTaskHandle)); // Task handle
Serial.print(xTaskCreate(vButtonTask, // Task function
"vButtonTask", // Task name,
1024, // 1 KiloBit
NULL, // Parameter
BUTTON_TASK_PRIORITY, // Task priority scale
&xButtonTaskHandle)); // Task handle
}
void loop()
{
// put your main code here, to run repeatedly:
//Serial.print("IodineGamingHD \n");
}