FreeRTOS Code
#include <Arduino_FreeRTOS.h>
int LEDpin1 = 12;
int LEDpin2 = 13;
void TaskLED1( void );
void TaskLED2( void );
void setup()
{
xTaskCreate(TaskLED1,"Blink1",128,NULL,1,NULL);
xTaskCreate(TaskLED2,"Blink2",128,NULL,2,NULL);
vTaskStartScheduler();
}
void loop()
{
// Empty. Things are done in Tasks.
}
void TaskLED1(void ) // This is a task.
{
pinMode(LEDpin1, OUTPUT);
for (;;)
{
digitalWrite(LEDpin1, HIGH); // turn the LED on
vTaskDelay( 50 ); // wait
digitalWrite(LEDpin1, LOW); // turn the LED off
vTaskDelay( 50 ); // wait
}
}
void TaskLED2(void ) // This is a task.
{
pinMode(LEDpin2, OUTPUT);
for (;;)
{
digitalWrite(LEDpin2, HIGH); // turn the LED on (HIGH is the voltage level)
vTaskDelay( 50 ); // wait for one second
digitalWrite(LEDpin2, LOW); // turn the LED off by making the voltage LOW
vTaskDelay( 50 ); // wait for one second
}
}
#include <Arduino_FreeRTOS.h>
int LEDpin1 = 12;
int LEDpin2 = 13;
void TaskLED1( void );
void TaskLED2( void );
void setup()
{
Serial.begin(9600);
Serial.println("In Setup function");
xTaskCreate(TaskLED1,"Blink1",128,NULL,2,NULL);
xTaskCreate(TaskLED2,"Blink2",128,NULL,1,NULL);
vTaskStartScheduler();
}
void loop()
{
// Empty. Things are done in Tasks.
}
void TaskLED1(void ) // This is a task.
{
pinMode(LEDpin1, OUTPUT);
Serial.println(" LED 1 Before");
for (;;)
{
Serial.println(" LED 1 After");
digitalWrite(LEDpin1, HIGH); // turn the LED on
vTaskDelay( 50 ); // wait
digitalWrite(LEDpin1, LOW); // turn the LED off
vTaskDelay( 50 ); // wait
}
}
void TaskLED2(void ) // This is a task.
{
pinMode(LEDpin2, OUTPUT);
Serial.println(" LED 2 Before");
for (;;)
{
Serial.println(" LED 2 After");
digitalWrite(LEDpin2, HIGH); // turn the LED on (HIGH is the voltage level)
vTaskDelay( 50 ); // wait for one second
digitalWrite(LEDpin2, LOW); // turn the LED off by making the voltage LOW
vTaskDelay( 50 ); // wait for one second
}
}
int LEDpin1 = 12;
int LEDpin2 = 13;
void TaskLED1(void);
void TaskLED2(void);
void setup() {
pinMode(LEDpin1, OUTPUT);
pinMode(LEDpin2, OUTPUT);
}
void loop() {
TaskLED1();
TaskLED2();
}
void TaskLED1(void) // This is a task.
{
digitalWrite(LEDpin1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for one second
digitalWrite(LEDpin1, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for one second
}
void TaskLED2(void) // This is a task.
{
digitalWrite(LEDpin2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500);
digitalWrite(LEDpin2, LOW); // turn the LED off by making the voltage LOW
delay(500);
}
//LED interfacing using Interrupt Service Routine (ISR)
#define LED1 9
#define LED2 10
#define SW1 2
#define SW2 3
void toggle(byte pinNum)
{
byte pinState;
pinState= digitalRead(pinNum);
pinState=!pinState;
digitalWrite(pinNum, pinState);
}
void setup()
{
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
digitalWrite(LED1, LOW); // LED off
digitalWrite(LED2, LOW); // LED off
pinMode(SW1, INPUT);
pinMode(SW2, INPUT);
attachInterrupt(digitalPinToInterrupt(2), LEDISR0, LOW); // interrupt 0 digital pin 2 connected SW0
attachInterrupt(digitalPinToInterrupt(3), LEDISR1, RISING); // interrupt 1 digital pin 3 connected SW1
}
void loop()
{
// do nothing
}
// can't use delay(x) in IRQ routine
void LEDISR0()
{
toggle(LED1);
}
void LEDISR1()
{
toggle(LED2);
}
// Sharing serial commuication link between two tasks using Semaphore
#include <Arduino_FreeRTOS.h>
#include <semphr.h>
// Declare a mutex Semaphore Handle which we will use to manage the Serial Port.
// It will be used to ensure only only one Task is accessing this resource at any time.
SemaphoreHandle_t xSerialSemaphore;
// define two Tasks for DigitalRead & AnalogRead
void TaskDigitalRead( void *pvParameters );
void TaskAnalogRead( void *pvParameters );
void setup()
{
Serial.begin(9600);
if ( xSerialSemaphore == NULL ) // Check to confirm that the Serial Semaphore has not already been
created.
{
xSerialSemaphore = xSemaphoreCreateMutex(); // Create a mutex semaphore we will use to manage
the Serial Port
if ( ( xSerialSemaphore ) != NULL )
xSemaphoreGive( ( xSerialSemaphore ) ); // Make the Serial Port available for use, by "Giving" the
Semaphore.
}
// Now set up two Tasks to run independently.
xTaskCreate(TaskDigitalRead, "DigitalRead", 128, NULL, 2, NULL );
xTaskCreate(TaskAnalogRead, "AnalogRead", 128, NULL, 1, NULL );
// Now the Task scheduler, which takes over control of scheduling individual Tasks, is automatically
started.
}
void loop()
{
// Empty. Things are done in Tasks.
}
void TaskDigitalRead( void *pvParameters __attribute__((unused)) ) // This is a Task.
{/*DigitalReadSerial, Reads a digital input on pin 2, prints the result to the serial monitor */
uint8_t pushButton = 2; // digital pin 2 has a pushbutton attached to it.
pinMode(pushButton, INPUT);
for (;;)
{
int buttonState = digitalRead(pushButton);
// See if we can obtain or "Take" the Serial Semaphore.
// If the semaphore is not available, wait 5 ticks of the Scheduler to see if it becomes free.
if ( xSemaphoreTake( xSerialSemaphore, ( TickType_t ) 5 ) == pdTRUE )
{
Serial.println(buttonState);
xSemaphoreGive( xSerialSemaphore ); // Now free or "Give" the Serial Port for others.
}
vTaskDelay(1); // one tick delay (15ms) in between reads for stability
}
}
void TaskAnalogRead( void *pvParameters __attribute__((unused)) ) // This is a Task.
{
for (;;)
{// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// See if we can obtain or "Take" the Serial Semaphore.
// If the semaphore is not available, wait 5 ticks of the Scheduler to see if it becomes free.
if ( xSemaphoreTake( xSerialSemaphore, ( TickType_t ) 5 ) == pdTRUE )
{
Serial.println(sensorValue);
xSemaphoreGive( xSerialSemaphore ); // Now free or "Give" the Serial Port for others.
}
vTaskDelay(1); // one tick delay (15ms) in between reads for stability
}
}
// This program creeates two tasks priority 1 and 2.
// It also creates an idle task is also created, which will be run when there are no tasks in RUN state
//#include <FreeRTOS.h>
#include<Arduino_FreeRTOS.h>
void setup()
{
Serial.begin(9600);
Serial.println("In Setup function")
/* Create three tasks with priorities 1 and 2 and IDLE. */
xTaskCreate(MyTask1, "Task1", 100, NULL, 1, NULL);
xTaskCreate(MyTask2, "Task2", 100, NULL, 2, NULL);
xTaskCreate(MyTask3, "Task3", 100, NULL, 2, NULL);
//xTaskCreate(MyIdleTask, "IdleTask", 100, NULL, 0, NULL);
}
void loop()
{
// DO nothing
}
/* Task2 with priority 2 */
static void MyTask2(void* pvParameters)
{
while(1)
{
Serial.println("Task 2");
vTaskDelay(250/portTICK_PERIOD_MS);
}
}
static void MyTask3(void* pvParameters)
{
while(1)
{
Serial.println("Task 3");
vTaskDelay(200/portTICK_PERIOD_MS);
}
}
/* Task1 with priority 1 */
static void MyTask1(void* pvParameters)
{
while(1)
{
Serial.println("Task 1");
vTaskDelay(200/portTICK_PERIOD_MS);
}
}
// This program demonstrates suspension and resuming of tasks
#include <Arduino_FreeRTOS.h>
TaskHandle_t TaskHandle_1; // Task handle of task 1
TaskHandle_t TaskHandle_2; // Task handle of task 2
TaskHandle_t TaskHandle_3; // Task handle of task 3
void setup()
{
Serial.begin(9600);
Serial.println("In Setup function");
/* Create 3-tasks with priorities 1-3. Capture the Task details to respective handlers */
xTaskCreate(MyTask1, "Task1", 100, NULL, 1, &TaskHandle_1); // It resumes all tasks and kill itself
xTaskCreate(MyTask2, "Task2", 100, NULL, 2, &TaskHandle_2); // It kill itself
xTaskCreate(MyTask3, "Task4", 100, NULL, 3, &TaskHandle_3); // It suspends task 2 and itself
}
void loop()
{
// Do nothing
}
/* Task1 with priority 1 */
static void MyTask1(void* pvParameters)
{
Serial.println("Task1 Running: and it Resumes Task2");
vTaskResume(TaskHandle_2);
Serial.println("Task1 Running: and it Resumes Task3");
vTaskResume(TaskHandle_3);
Serial.println("Task1 Running: and Deleting Itself");
vTaskDelete(TaskHandle_1);
}
/* Task2 with priority 2 */
static void MyTask2(void* pvParameters)
{
Serial.println("Task2 Running: and Deleting Itself");
vTaskDelete(NULL); //Delete own task by passing NULL(TaskHandle_2 can also be used)
}
/* Task3 with priority 3 */
static void MyTask3(void* pvParameters)
{
Serial.println("Task3 Running: and Suspending Task 2 and Itself");
vTaskSuspend(TaskHandle_2); //Suspend Task2
vTaskSuspend(NULL); //Suspend Own Task
Serial.println("Back in Task3: and Deleting Itself");
vTaskDelete(TaskHandle_3);
}
// This program creeates two tasks priority 1 and 2.
#include <Arduino_FreeRTOS.h>
void setup()
{
Serial.begin(9600);
Serial.println("In Setup function");
/* Create three tasks with priorities 1 and 2 */
xTaskCreate(MyTask1, "Task1", 100, NULL, 1, NULL);
xTaskCreate(MyTask2, "Task2", 100, NULL, 2, NULL);
}
void loop()
{
// DO nothing
}
/* Task1 with priority 1 */
static void MyTask1(void* pvParameters)
{
while(1)
{
Serial.println("Executing Task 1");
vTaskDelay(250/portTICK_PERIOD_MS);
}
}
/* Task2 with priority 2 */
static void MyTask2(void* pvParameters)
{
while(1)
{
Serial.println("Executing Task 2");
vTaskDelay(50/portTICK_PERIOD_MS);
}
}
/* Idle Task with priority Zero
static void MyIdleTask(void* pvParameters)
{
while(1)
{
Serial.println("Idle Task running: Task 1 & Task 2 in Block state");
delay(50);
}
}*/
// Without Semaphore
#include <Arduino_FreeRTOS.h>
#define LED 13
void setup()
{
Serial.begin(9600);
pinMode(LED ,OUTPUT);
xTaskCreate(LedOnTask, "LedON",100,NULL,1,NULL);
xTaskCreate(LedoffTask, "LedOFF", 100,NULL,1,NULL);
}
void loop(){}
void LedOnTask(void *pvParameters)
{
while(1)
{
Serial.println("Inside LedOnTask");
digitalWrite(LED,LOW);
vTaskDelay(1);
}
}
void LedoffTask(void *pvParameters)
{
while(1)
{
Serial.println("Inside LedOffTask");
digitalWrite(LED,HIGH);
vTaskDelay(1);
}
}
//With Semaphore
#include <Arduino_FreeRTOS.h>
#include "semphr.h"
#define LED 13
SemaphoreHandle_t xBinarySemaphore;
void setup()
{
Serial.begin(9600);
pinMode(LED ,OUTPUT);
xBinarySemaphore = xSemaphoreCreateBinary();
xTaskCreate(LedOnTask, "LedON",100,NULL,1,NULL);
xTaskCreate(LedoffTask, "LedOFF", 100,NULL,1,NULL);
xSemaphoreGive(xBinarySemaphore);
}
void loop(){}
void LedOnTask(void *pvParameters)
{
while(1)
{
xSemaphoreTake(xBinarySemaphore,portMAX_DELAY);
Serial.println("Inside LedOnTask");
digitalWrite(LED,LOW);
xSemaphoreGive(xBinarySemaphore);
vTaskDelay(1);
}
}
void LedoffTask(void *pvParameters)
{
while(1)
{
xSemaphoreTake(xBinarySemaphore,portMAX_DELAY);
Serial.println("Inside LedOffTask");
digitalWrite(LED,HIGH);
xSemaphoreGive(xBinarySemaphore);
vTaskDelay(1);
}
}