Project - Implement the Structure Queue method by using Free RTOS on ARDUINO UNO to detect running temperatures of two motors parallelly and display the same on a 16x02 LCD display
/* By using structure queue concept in Free RTOS,
Displaying temperatures of two running motors with help of ARDUINO UNO, TEMP SENSORS, LCD Display */
//header files inclusion
#include <Arduino_FreeRTOS.h>
#include <LiquidCrystal.h>
#include <queue.h>
QueueHandle_t sQueue;
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);//{RS, EN, D4, D5, D6, D7}// Initializing LCD
struct pinRead
{
int pin; // analog channel indentifier
float value; // sensor output value
};
void Tempsense1(void *pvParameters);
void Tempsense2(void *pvParameters);
void LCDPRINT(void *pvParameters);
void setup()
{
Serial.begin(9600);
sQueue = xQueueCreate( 5, sizeof( struct pinRead ) ); //Creating structure queue
if( sQueue != NULL )
{
xTaskCreate( Tempsense1, "ROOM1", 240, NULL, 1, NULL ); //creating tasks
xTaskCreate( Tempsense2, "ROOM2", 240, NULL, 1, NULL );
xTaskCreate( LCDPRINT, "LCD", 240, NULL, 2, NULL );
vTaskStartScheduler();
}
else
{
Serial.println("The queue could not be created");
}
}
voidloop()
{ }
void Tempsense1(void *pvParameters)
{
for(;;) //infinite loop
{
int analogValue = analogRead(A0); //reading temp sensor 1 value
const float BETA = 3950; //beta coefficient of temp sensor
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
//converting sensor value into degree c
Serial.print("Motor 1 temperature: ");
Serial.print(celsius);
Serial.print(" ℃\n");
delay(100);
struct pinRead temp1; // define a structure of type pinRead
temp1.pin = 0; // assign value '0' to pin element of struct
temp1.value = celsius; // load celsius value in value element of struct
xQueueSend(sQueue, &temp1, portMAX_DELAY== pdPASS); //write struct message to queue
taskYIELD(); // terminate the task and inform the scheduler about it
}
}
void Tempsense2(void *pvParameters)
{
for(;;)
{
int analogValue2 = analogRead(A1); //reading temp sensor 2 value
const float BETA2 = 3950; //beta coefficient of temp sensor
float celsius2 = 1 / (log(1 / (1023. / analogValue2 - 1)) / BETA2 + 1.0 / 298.15) - 273.15;
Serial.print("Motor 2 temperature: ");
Serial.print(celsius2);
Serial.print(" ℃\n");
delay(100);
struct pinRead temp2; // define a structure of type pinRead
temp2.pin = 1; // assign value '1' to pin element of struct
temp2.value = celsius2; // load celsius2 value in value element of struct
xQueueSend(sQueue, &temp2, portMAX_DELAY== pdPASS); //write struct message to queue
taskYIELD(); // terminate the task and inform the scheduler about it
}
}
void LCDPRINT(void *pvParameters)
{
for(;;)
{
float temp1=0,temp2=0;
struct pinRead channel;
if (xQueueReceive(sQueue, &channel, portMAX_DELAY) == pdPASS) //Reading data from queue
{
// print received data elements on serial montor
Serial.print("\nPin: ");
Serial.print(channel.pin);
Serial.print(" Value: ");
Serial.print(channel.value);
Serial.print("\n");
// If condition checks, if data receive from channel zero
if(channel.pin==0) //checking for which channel input
{
lcd.begin(16, 2); //initializing lcd for 16 columns and 2 rows
lcd.print("Motor 1"); //printing message in row 0
lcd.setCursor(0,1); //setting cursor in 2nd row first column position
lcd.print("Temp : ");
lcd.setCursor(7,1);
lcd.print(channel.value);
lcd.setCursor(13,1);
lcd.print("\xDF C");// "\xDF C" will be printed as degree symbol C
}
if(channel.pin==1)
{
lcd.begin(16, 2);
lcd.print("Motor 2 ");
lcd.setCursor(0,1);
lcd.print("Temp : ");
lcd.setCursor(7,1);
lcd.print(channel.value);
lcd.setCursor(13,1);
lcd.print("\xDF C");
}
delay(1000); //introducing intentional delay to see the results clearly on LCD screen.
}
taskYIELD(); // terminate the task and inform the scheduler about it
}
}