FreeRTOS Code
FreeRTOS Code
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 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 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;
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.
}
void loop()
{
// Empty. Things are done in Tasks.
}
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);
//#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
}
#include <Arduino_FreeRTOS.h>
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);
#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
}
void loop(){}
void loop(){}