01 Basic Example of FreeRTOS with Arduino
FreeRTOS:
FreeRTOS is a real-time operating system kernel for embedded device. It is distributed under the MIR license. In this tutorial, we will start with FreeRTOS. FreeRTOS is a class of RTOS for embedded devices which is small enough to be run on 8/16 bit microcontrollers. It is completely open source. If we know some basic concepts of FreeRTOS then it is very easy to use because FreeRTOS is well documented APIs which can be directly used in the code without knowing the backend part of the coding. It can also be run on Arduino Uno board. We have to just download the FreeRTOS library and install then start implementing the code using APIs.
Install FreeRTOS Library:
1. Open Arduino IDE and go to Sketch -> Include Library -> Manage Libraries. Search for FreeRTOS and install the library as shown below.
Schematic Diagram:
This is schematic diagram, I am using one Arduino Nano, two LEDs and two 220 Ohms resistor. You can see in schematic diagram Anode(Positive) pin of LED is connected 7th and 8th digital pin and resistor is connected in series between Led's Anode pin and Arduino digital pin. Cathode pin of Leds are common of both Leds and connected to GND pin of Arduino Nano.
Let us Code:
#include <Arduino_FreeRTOS.h>
#define redLed 7
#define greenLed 8
void setup() {
xTaskCreate(redLedTask, "red Led Task", 128, NULL, 1, NULL);
xTaskCreate(greenLedTask, "green Led Task", 128, NULL, 1, NULL);
}
void greenLedTask(void *pvParameters){
pinMode(greenLed, OUTPUT);
while(1){
digitalWrite(greenLed, HIGH);
delay(200);
digitalWrite(greenLed, LOW);
delay(200);
}
}
void redLedTask(void *pvParameters){
pinMode(redLed, OUTPUT);
while(1){
digitalWrite(redLed, HIGH);
delay(500);
digitalWrite(redLed, LOW);
delay(500);
}
}
#include <Arduino_FreeRTOS.h>
#define redLed 7
#define greenLed 8
void setup() {
xTaskCreate(redLedTask, "red Led Task", 128, NULL, 1, NULL);
xTaskCreate(greenLedTask, "green Led Task", 128, NULL, 1, NULL);
}
void loop() {}
void redLedTask(void *pvParameters){
pinMode(redLed, OUTPUT);
while(1){
digitalWrite(redLed, HIGH);
delay(500);
digitalWrite(redLed, LOW);
delay(500);
}
}
void greenLedTask(void *pvParameters){
pinMode(greenLed, OUTPUT);
while(1){
digitalWrite(greenLed, HIGH);
delay(200);
digitalWrite(greenLed, LOW);
delay(200);
}
}
No comments: