//Structure Queue - v1
// Include Arduino FreeRTOS library
#include <Arduino_FreeRTOS.h>// include LCD library function
#include<LiquidCrystal.h>//Define LCD pins
LiquidCrystal lcd (7,6,5,4,3,2); //RS, E, D4, D5, D6, D7
// Include queue support
#include <queue.h>
// Define a struct
struct pinRead
{
int pin; // analog channel indentifier
int value; //sensor output value
};
QueueHandle_t structQueue;
void setup()
{
Serial.begin(9600); //Enable serial communication library.
lcd.begin(16, 2); //Enable LCD library
//create Structure Queue
structQueue = xQueueCreate(10, sizeof(struct pinRead)); // Queue length, Queue item size
if (structQueue != NULL) {
// Create task that consumes the queue if it was created.
xTaskCreate(TaskLcd,"Displaydata", 128,NULL,2,NULL); // Task function
/* A
name just for humans, This stack size can be checked & adjusted by
reading the Stack Highwater, Priority, with 3 (configMAX_PRIORITIES - 1) being the
highest, and 0 being the lowest.*/
// Create task that publish data in the queue if it wascreated.
xTaskCreate(TaskTempReadPin0,"AnalogReadPin0",128,NULL,1,NULL); // Task function
// Create other task that publish data in the queue if it was created.
// xTaskCreate(TaskLightReadPin1, "AnalogReadPin1", 128, NULL,1, NULL);
}
vTaskStartScheduler();
}
void loop()
{
}
// Temperature value and adc number writing
void TaskTempReadPin0(void *pvParameters)
{
(void) pvParameters;
for (;;)
{
struct pinRead currentPinRead;
// define a structure of type pinRead
currentPinRead.pin = 0; // assign value '0' to pin element of struct
currentPinRead.value = analogRead(A0); // Read adc value from A0 channel and store it in valueelement of struct
xQueueSend(structQueue,¤tPinRead, portMAX_DELAY); //write struct message to queue
Serial.println("Channel_0"); //printChannel_0 on serial monitor
taskYIELD(); //terminate the task and inform schulder about it
}
}
void TaskLcd(void * pvParameters)
{
(void) pvParameters;
for (;;)
{
int TEMP=0; // temporary variable to hold Temperature adc0 value
struct pinRead currentPinRead;
// structure to hold receiv data
// Read structure elements from queue and check if datareceived successfully
if(xQueueReceive(structQueue, ¤tPinRead, portMAX_DELAY) == pdPASS)
{
// print received data elements on serial montor
Serial.print("Pin: ");
Serial.print(currentPinRead.pin);
Serial.print(" Value: ");
Serial.println(currentPinRead.value);
// If condition checks, if data receive fromchannel zero
// If yes, store sensor value member of structure intemporary temp variable
if(currentPinRead.pin==0)
{
TEMP = (currentPinRead.value* 500)/1024; // convert adc value into temperature
// dispay temperature sensor output in first line of 16x2LCD
lcd.setCursor(0, 0);
lcd.print("ROOMTemp = ");
lcd.setCursor(7, 0);
lcd.print(TEMP);
lcd.print("'C");
}
taskYIELD(); // terminate the task and inform schulder about it
}
}
}