#include <Arduino_FreeRTOS.h>
// Define all the needed compile time constants
#define T1_EXECUTION_TIME 1050
#define T2_EXECUTION_TIME 2100
#define T3_EXECUTION_TIME 1050
#define T4_EXECUTION_TIME 1050
#define TICK_DELAY_TIME 15
#define L1_PIN 12
#define L2_PIN 11
#define L3_PIN 10
#define THRESHOLD_1 512
#define THRESHOLD_2 768
// Define all the needed variables
int potPin = 0; // Analog pin used to connect the potentiometer
int manualPotPin = 1; // Analog pin used to connect the manual potentiometer
int potVal; // Variable to read the potentiometer value from the analog pin
int mode; // The functioning mode of the APPL: 0 = AUTO / 1 = MANUAL
int manualPotVal; // Variable to read the manual potentiometer value from the analog pin
// Define all the needed Tasks
void T1( void *pvParameters );
void T2( void *pvParameters );
void T3( void *pvParameters );
void T4( void *pvParameters );
// Define the handles for the tasks
TaskHandle_t T1Handle;
TaskHandle_t T2Handle;
TaskHandle_t T3Handle;
TaskHandle_t T4Handle;
// The setup function runs once when we press reset or power the board
void setup()
{
// Initialize the digital pins used for the 3 LEDS as outputs
pinMode(L1_PIN, OUTPUT);
pinMode(L2_PIN, OUTPUT);
pinMode(L3_PIN, OUTPUT);
// Initialize the pin used for the built-in LED as output
pinMode(LED_BUILTIN, OUTPUT);
// Initialize the SWITCH pins as inputs
pinMode(A2, INPUT_PULLUP);
pinMode(A3, INPUT_PULLUP);
// Initialize the serial communication at 9600 bits per second
Serial.begin(9600);
// Set up the Tasks to run independently
xTaskCreate(
T1
, "T1 - Read analog entry" // Task name
, 128 // Stack size - can be checked & adjusted by reading the Stack Highwater
, NULL // Parameters for the task
, 3 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest
, &T1Handle ); // Task Handle
xTaskCreate(
T2
, "T2 - LED Command" // Task name
, 128 // Stack size
, NULL // Parameters for the task
, 1 // Priority
, &T2Handle ); // Task Handle
xTaskCreate(
T3
, "T3 - Send to serial interface" // Task name
, 128 // Stack size
, NULL // Parameters for the task
, 1 // Priority
, &T3Handle ); // Task Handle
xTaskCreate(
T4
, "T4 - Mode Switch" // Task name
, 128 // Stack size
, NULL // Parameters for the task
, 1 // Priority
, &T4Handle ); // Task Handle
// Now the Task scheduler is automatically started.
// It takes control of scheduling the individual Tasks.
}
void loop()
{
// Read the mode value based on the switch position: 0 = AUTO / 1 = MANUAL
mode = digitalRead(A2);
// In AUTO mode
if (mode == 0)
{
// Resume T1, T2 and T3
vTaskResume( T1Handle );
vTaskResume( T2Handle );
vTaskResume( T3Handle );
// Suspend T4
vTaskSuspend( T4Handle );
// Turn the built-in LED OFF
digitalWrite(LED_BUILTIN, LOW);
}
// In MANUAL mode
else
{
// Suspend T1, T2 and T3
vTaskSuspend( T1Handle );
vTaskSuspend( T2Handle );
vTaskSuspend( T3Handle );
// Resume T4
vTaskResume( T4Handle );
// Turn the built-in LED ON
digitalWrite(LED_BUILTIN, HIGH);
}
}
/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/
void T1( void *pvParameters __attribute__((unused)) )
{
for (;;)
{
// Reads the value of the potentiometer (value between 0 and 1023)
potVal = analogRead(potPin);
// Add the required delay between reads. This is given in clock ticks, not millis
vTaskDelay(T1_EXECUTION_TIME/TICK_DELAY_TIME);
}
}
void T2( void *pvParameters __attribute__((unused)) )
{
for (;;)
{
if(potVal < THRESHOLD_1)
{
// Turn LED 1 ON (HIGH is the voltage level)
digitalWrite(L1_PIN, HIGH);
// Turn LED 2 and 3 OFF by making the voltage LOW
digitalWrite(L2_PIN, LOW);
digitalWrite(L3_PIN, LOW);
}
else if ((potVal >= THRESHOLD_1) && (potVal < THRESHOLD_2))
{
// Turn LED 1 and 2 ON (HIGH is the voltage level)
digitalWrite(L1_PIN, HIGH);
digitalWrite(L2_PIN, HIGH);
// Turn LED 3 OFF by making the voltage LOW
digitalWrite(L3_PIN, LOW);
}
else
{
// Turn LED 1, 2 and 3 ON (HIGH is the voltage level)
digitalWrite(L1_PIN, HIGH);
digitalWrite(L2_PIN, HIGH);
digitalWrite(L3_PIN, HIGH);
}
// Add the required delay between reads. This is given in clock ticks, not millis
vTaskDelay(T2_EXECUTION_TIME/TICK_DELAY_TIME);
}
}
void T3( void *pvParameters __attribute__((unused)) )
{
for (;;)
{
// Send over the read analog data to the serial interface
Serial.print("Acquisition value : "); Serial.println(potVal);
// Add the required delay between reads. This is given in clock ticks, not millis
vTaskDelay(T3_EXECUTION_TIME/TICK_DELAY_TIME);
}
}
void T4( void *pvParameters __attribute__((unused)) )
{
for (;;)
{
// Reads the value of the manual potentiometer (value between 0 and 1023)
manualPotVal = analogRead(manualPotPin);
if(manualPotVal < THRESHOLD_1)
{
// Turn LED 1 ON (HIGH is the voltage level)
digitalWrite(L1_PIN, HIGH);
// Turn LED 2 and 3 OFF by making the voltage LOW
digitalWrite(L2_PIN, LOW);
digitalWrite(L3_PIN, LOW);
}
else if ((manualPotVal >= THRESHOLD_1) && (manualPotVal < THRESHOLD_2))
{
// Turn LED 1 and 2 ON (HIGH is the voltage level)
digitalWrite(L1_PIN, HIGH);
digitalWrite(L2_PIN, HIGH);
// Turn LED 3 OFF by making the voltage LOW
digitalWrite(L3_PIN, LOW);
}
else
{
// Turn LED 1, 2 and 3 ON (HIGH is the voltage level)
digitalWrite(L1_PIN, HIGH);
digitalWrite(L2_PIN, HIGH);
digitalWrite(L3_PIN, HIGH);
}
// Check if we are in manual mode
if (mode == 1)
{
// Send over the read analog data to the serial interface
Serial.println("Manual mode!");
Serial.print("Acquisition value : "); Serial.println(manualPotVal);
}
// Add the required delay between reads. This is given in clock ticks, not millis
vTaskDelay(T4_EXECUTION_TIME/TICK_DELAY_TIME);
}
}