/*
EMBEDDED C PROJECT
SRN: PES2UG21EC137
Name: Shubham Mookim
(I've given a breifing how I did it)
Topic: Embedded System for Flow Control and SOS Monitoring
Description: This project involves the development of an embedded system using an
Arduino board to control flow and monitor emergency situations. The system
utilizes FreeRTOS, Servo library, and task-based programming to achieve interactive
functionality.
Tasks:
1. DigitalRead Task: Monitors the state of an emergency button and toggles a solenoid
valve accordingly.
2. AnalogRead Task: Reads data from a flow meter and adjusts the sensor value based on
the solenoid valve status.
3. Servo Task: Controls a motorized valve (servo) based on the adjusted sensor value.
Display Monitor Task: Displays real-time information including the emergency status,
flow meter value, and servo value.
Inter-Task Communication:
The tasks communicate with each other using a binary semaphore (xSerialSemaphore)
implemented with FreeRTOS. This semaphore ensures exclusive access to shared
resources and allows tasks to synchronize their execution and exchange data.
*/
#include <Arduino_FreeRTOS.h>
#include <semphr.h>
#include <Servo.h>
Servo myservo; // to control a servo
SemaphoreHandle_t xSerialSemaphore;
int sensorPin = A0;
int sensorValue = 0;
int servoValue = 0;
bool SelenoidNC = false;
// DigitalRead & AnalogRead Sub routine
void TaskDigitalRead( void *pvParameters );
void TaskAnalogRead( void *pvParameters );
void displayMonitor( void *pvParameters);
void servoTask( void *pvParameters);
// function runs once when you power the board
void setup() {
myservo.attach(9);
Serial.begin(9600);
while (!Serial) {
;
}
{
xSerialSemaphore = xSemaphoreCreateMutex();
if ( ( xSerialSemaphore ) != NULL )
xSemaphoreGive( ( xSerialSemaphore ) );
}
// 3 task that run independant as per the question
xTaskCreate(
TaskDigitalRead
, "DigitalRead - SOS "
, 128
, NULL
, 1
, NULL );
xTaskCreate(
TaskAnalogRead
, "AnalogRead - Flow Meter"
, 128
, NULL
, 2
, NULL );
xTaskCreate(displayMonitor, "Display - Monitoring", 128, NULL, 4, NULL);
xTaskCreate(servoTask, "Display - Monitoring", 128, NULL, 3, NULL);
}
void loop()
{
}
/*---------------------- Tasks ---------------------*/
void TaskDigitalRead( void *pvParameters __attribute__((unused)) )
{
uint8_t pushButton = 0;
pinMode(pushButton, INPUT);
for (;;)
{
int buttonState = digitalRead(pushButton);
if ( xSemaphoreTake( xSerialSemaphore, ( TickType_t ) 5 ) == pdTRUE )
{
if (buttonState == LOW){
SelenoidNC = !SelenoidNC;
vTaskDelay(100);
}
xSemaphoreGive( xSerialSemaphore );
}
vTaskDelay(10);
}
}
void TaskAnalogRead( void *pvParameters __attribute__((unused)) )
{
for (;;)
{
if ( xSemaphoreTake( xSerialSemaphore, ( TickType_t ) 5 ) == pdTRUE )
{
sensorValue = analogRead(sensorPin);
if(SelenoidNC == true){
sensorValue = 0;
}
xSemaphoreGive( xSerialSemaphore );
}
vTaskDelay(10);
}
}
void servoTask( void *pvParameters __attribute__((unused)) )
{
for (;;)
{
int K = 1;
servoValue = sensorValue * K;
if ( xSemaphoreTake( xSerialSemaphore, ( TickType_t ) 5 ) == pdTRUE )
{
servoValue = map(servoValue, 0, 1024, 0, 181);
myservo.write(servoValue);
if(SelenoidNC == true){
myservo.write(0);
}
xSemaphoreGive( xSerialSemaphore );
}
vTaskDelay(10);
}
}
void displayMonitor( void *pvParameters __attribute__((unused)) )
{
for(;;)
{
if ( xSemaphoreTake( xSerialSemaphore, ( TickType_t ) 5 ) == pdTRUE )
{
if (!SelenoidNC){
Serial.print("Emergency Status : ");
Serial.println(" SAFE");
Serial.print("Flow Meter Value (%):\t");
Serial.print(sensorValue);
Serial.print(" Servo Value (deg):\t");
Serial.println(servoValue);
}else{
Serial.print("Emergency Status :\t");
Serial.println("ALERT!!!");
}
xSemaphoreGive( xSerialSemaphore );
}
vTaskDelay(pdMS_TO_TICKS(1000));
}
}