Hello Everyone, So I was trying to implement the FreeRTOS along with Arduino by setting up a simple circuit.
Working: I have two LED's - Blue (Pin 11) and Red (Pin 12). So, My idea was to make Red Led as a lower priority task and the Blue Led will be the higher priority task which will turn on when the potentiometer is rotated (I used Analog Comparator for this instance). When the poteniometer is rotated, it runs the in-built function called enableinterrupt()
which is inside of the analogComp
library. So, when the interrupt is raised I try to create an Event by setting BIT_0 of the Event Group to 1 which leads to Blue LED to light up.
Results: I do see the Blue LED lighting up when I rotate the potentiometer. But there is something I noticed in two instances:
1.) When I keep the Red LED just HIGH, The Blue LED lights up and goes low but the Red stays high during whole time.
2.) When I toggle the Red LED high to low and also incorporate delays within, I still see that for some time the both LED's are on.
I was under the impression that since the Blue LED is of a higher priority task, it would lead the lower priority task to be suspended while it finishes execution which I am not able to see with my naked eye. So, to resolve this Can you please suggest what I should be doing so that I becomes evident to the visible eye that Red Led should be off when Blue Led is on?
Here is a code that I am running:
#include <Arduino_FreeRTOS.h>
#include <event_groups.h>
#include <analogComp.h>
// define two tasks for Blink & AnalogRead
void TaskBlink( void *pvParameters );
void TaskAnalogRead( void *pvParameters );
//define function call for setting bits for Event group
void aFunction(EventGroupHandle_t xEventGroup);
//Declaring a variable to hold the event group which will be created
EventGroupHandle_t xCreatedEventGroup;
//define bit for event
#define BIT_0 (1<<0)
// the setup function runs once when you press reset or power the board
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB, on LEONARDO, MICRO, YUN, and other 32u4 based boards.
}
//We create a event group
xCreatedEventGroup = xEventGroupCreate();
//If the event group was not created successfully.
if(xCreatedEventGroup == NULL){
Serial.println("The event group did not get created.");
}
//if the event group was created succesfully.
else {
//aFunction(xCreatedEventGroup);
analogComparator.setOn(AIN0,AIN1);
analogComparator.enableInterrupt(aFunction,CHANGE);
}
// Now set up two tasks to run independently.
xTaskCreate(
TaskBlink
, "Blink" // A name just for humans
, 128 // This stack size can be checked & adjusted by reading the Stack Highwater
, NULL
, 1 // Priority, with 1 (configMAX_PRIORITIES - 1) 3 being the highest, and 0 being the lowest.
, NULL );
xTaskCreate(
TaskAnalogRead
, "AnalogRead"
, 128 // Stack size
, NULL
, 2 // Priority
, NULL );
// Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
}
void loop()
{
// Empty. Things are done in Tasks.
}
/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/
void TaskBlink(void *pvParameters) // This is a task.
{
(void) pvParameters;
pinMode(12,OUTPUT);
for (;;) // A Task shall never return or exit.
{
digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level)
vTaskDelay(pdMS_TO_TICKS(1000) ); // wait for one second
digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
vTaskDelay( pdMS_TO_TICKS(1000) ); // wait for one second
}
}
void TaskAnalogRead(void *pvParameters) // This is a task.
{
(void) pvParameters;
EventBits_t uxBit;
for (;;)
{
uxBit = xEventGroupWaitBits(xCreatedEventGroup, BIT_0, pdTRUE, pdFALSE, portMAX_DELAY);
if((uxBit & BIT_0) != 0){
digitalWrite(11,HIGH);
vTaskDelay(pdMS_TO_TICKS(500));
digitalWrite(11,LOW);
}
}
}
void aFunction(){
xEventGroupSetBitsFromISR(xCreatedEventGroup,BIT_0,pdFALSE);
}