#include <Arduino_FreeRTOS.h>
#include <task.h>
#include <semphr.h> // add the FreeRTOS functions for Semaphores (or Flags).
#include <FreeRTOSConfig.h>
//Needs the patched RTOS Library for the task Tags and the fft library
SemaphoreHandle_t xSerialSemaphore;
#define LEDPIN 13
#define LEDPIN2 12
// define two Tasks for DigitalRead & AnalogRead
void TaskLEDOn( void *pvParameters );
void TaskLEDOff( void *pvParameters );
void MyIdleTask(void *pvParameter );
// the setup function runs once when you press reset or power the board
void setup() {
pinMode(LEDPIN, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
unsigned long time;
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
time = micros();
//prints time since program started
// Semaphores are useful to stop a Task proceeding, where it should be paused to wait,
// because it is sharing a resource, such as the Serial port.
// Semaphores should only be used whilst the scheduler is running, but we can set it up here.
if ( xSerialSemaphore == NULL ) // Check to confirm that the Serial Semaphore has not already been created.
{
xSerialSemaphore = xSemaphoreCreateBinary(); // Create a binary semaphore for protecting access to the serial port, or a serial semaphore we will use to manage the task order
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(
TaskLEDOn
, "LEDOn" // A name just for humans
, 128 // This stack size can be checked & adjusted by reading the Stack Highwater
, NULL
, 2 // Priority, with 4 being the highest, and 0 being the lowest.
, NULL );
xTaskCreate(
TaskLEDOff
, "LEDOff"
, 128 // Stack size
, NULL
, 2 // Priority
, NULL );
xTaskCreate(MyIdleTask, "IdleTask", 100, NULL, 0, NULL);
// Now the Task scheduler, which takes over control of scheduling individual Tasks, is automatically started.
vTaskStartScheduler();
}
void loop()
{
// Empty. Things are done in Tasks.
}
/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/
void TaskLEDOn( void *pvParameters __attribute__((unused)) ) // This is a Task.
{
//Serial.println(**bufferTemp);
unsigned long time;
/* This task is going to be represented by a 1 i.e to set pin 5 to 1(ON). */ /*modified by Aradhana Dhumal*/
/* vTaskSetApplicationTaskTag( NULL, ( void * )HIGH );*/
vTaskSetApplicationTaskTag( NULL, ( void * )12);
for (;;) // A Task shall never return or exit.
{
// See if we can obtain or "Take" the Serial Semaphore.
// If the semaphore is not available, wait 10 ticks of the Scheduler to see if it becomes free.
//if ( xSemaphoreTake( xSerialSemaphore, ( TickType_t ) 10 ) == pdTRUE )
if ( xSemaphoreTake( xSerialSemaphore, portMAX_DELAY))
{
// We were able to obtain or "Take" the semaphore and can now access the shared resource.
// We want to have the Serial Port for us alone, as it takes some time to print,
// so we don't want it getting stolen during the middle of a conversion.
// print out the state of the button:
//Serial.println(buttonState);
//switch LED 13 on:
digitalWrite(LEDPIN, HIGH);
Serial.println("A111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111O");
//delay(20);
//xSemaphoreGive( xSerialSemaphore ); // Now free or "Give" the Serial Port for others.
}
xSemaphoreGive( xSerialSemaphore ); // Now free or "Give" the Serial Port for others.
//delay(30); //to sort out counting issues. //delay is blocking. it will have at this line and execute nothing .Processor will just be blocked// Modified by Aradhana 10-11-19
vTaskDelay(pdMS_TO_TICKS(150)); // delay (150ms) in between reads for stability //blocking- releases resources Modified by Aradhana 10-11-19
}
}
void TaskLEDOff( void *pvParameters __attribute__((unused)) ) // This is a Task.
{
TickType_t xLastWakeTime;
const TickType_t xFrequency = 3; //wait for at least 1 ticks
unsigned long time;
// Initialise the xLastWakeTime variable with the current time.
xLastWakeTime = xTaskGetTickCount();
/* This task is going to be represented by setting pin 6 to 1(ON). */ /*modified by Aradhana Dhumal*/
/*vTaskSetApplicationTaskTag( NULL, ( void * ) 0 );*/
vTaskSetApplicationTaskTag( NULL, ( void * )11);
for (;;)
{
// See if we can obtain or "Take" the Serial Semaphore.
// If the semaphore is not available, wait 10 ticks of the Scheduler to see if it becomes free.
//if ( xSemaphoreTake( xSerialSemaphore, ( TickType_t ) 10 ) == pdTRUE )
if ( xSemaphoreTake( xSerialSemaphore, portMAX_DELAY))
{
// We were able to obtain or "Take" the semaphore and can now access the shared resource.
// We want to have the Serial Port for us alone, as it takes some time to print,
// so we don't want it getting stolen during the middle of a conversion.
// print out the value you read:
//switch LED off
digitalWrite(LEDPIN, LOW);
Serial.println("A222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222O");
//vTaskDelay(2);
delay(20);
xSemaphoreGive( xSerialSemaphore ); // Now free or "Give" the Serial Port for others.
}
//xSemaphoreGive( xSerialSemaphore ); // Now free or "Give" the Serial Port for others.
//delay(20);
vTaskDelay(pdMS_TO_TICKS(150));
}
}
/* Idle Task with priority Zero */
static void MyIdleTask(void* pvParameters)
{
TickType_t xLastWakeTime;
const TickType_t xFrequency = 2; //wait for at least 1 tick
unsigned long time;
// Initialise the xLastWakeTime variable with the current time.
xLastWakeTime = xTaskGetTickCount();
/* This task is going to be represented by a voltage scale of 3. */
/*vTaskSetApplicationTaskTag( NULL, ( void * ) LOW );*/
vTaskSetApplicationTaskTag( NULL, ( void * )10);
for( ;; )
{
digitalWrite(LEDPIN, !digitalRead(LEDPIN));
delay(1);
}
}