//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
 xTaskCreate(TaskTempReadPin2,"AnalogReadPin2",128,NULL,1,NULL);
 
 // Create other task that publish data in the queue if it was created.
 xTaskCreate(TaskLightReadPin1,  "AnalogReadPin1", 128, NULL,1, NULL);
 xTaskCreate(TaskLightReadPin3,  "AnalogReadPin3", 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,&currentPinRead, 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 TaskTempReadPin2(void *pvParameters)
{
 (void) pvParameters;
for (;;)
{
 struct pinRead currentPinRead;
 // define a structure of type pinRead
 currentPinRead.pin = 2; // assign value '2' to pin element of struct
 currentPinRead.value = analogRead(A2); // Read adc value from A0 channel and store it in valueelement of struct


 xQueueSend(structQueue,&currentPinRead, portMAX_DELAY); //write struct message to queue
 Serial.println("Channel_00"); //printChannel_00 on serial monitor
  taskYIELD(); //terminate the task and inform schulder about it
  }
}
// Light Sensor value and adc number writing
void TaskLightReadPin1(void *pvParameters)
{
  (void) pvParameters;
  for (;;)
  {
    struct pinRead currentPinRead;
// define a structure of type pinRead
    currentPinRead.pin = 1; // assign value '1' to pin element of struct
    currentPinRead.value = analogRead(A1);// Read adc value from A1 channel and store it in value element of struct
    xQueueSend(structQueue,&currentPinRead, portMAX_DELAY); //write struct message to queue
    Serial.println("Channel_1");//printChannel_1 on serial monitor
    taskYIELD(); //terminate the task and inform schulder about it
  }
}
void TaskLightReadPin3(void *pvParameters)
{
  (void) pvParameters;
  for (;;)
  {
    struct pinRead currentPinRead;
// define a structure of type pinRead
    currentPinRead.pin = 3; // assign value '3' to pin element of struct
    currentPinRead.value = analogRead(A3);// Read adc value from A1 channel and store it in value element of struct
    xQueueSend(structQueue,&currentPinRead, portMAX_DELAY); //write struct message to queue
    Serial.println("Channel_11");//printChannel_11 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
   int LDR=0; // temporary variable to hold LDR adc1 value
   
    struct pinRead currentPinRead;
// structure to hold receiv data
// Read structure elements from queue and check if datareceived successfully
    if(xQueueReceive(structQueue, &currentPinRead, 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("Temp = ");
      lcd.setCursor(7, 0);
      lcd.print(TEMP);
      lcd.print("'C");
      }
 if(currentPinRead.pin==2)
      {
       TEMP = (currentPinRead.value* 500)/1024; // convert adc value into temperature
// dispay temperature sensor output in first line of 16x2LCD  
      lcd.setCursor(0, 1);
      lcd.print("Temp = ");
      lcd.setCursor(7, 0);
      lcd.print(TEMP);
      lcd.print("'C");
      }
      // If condition checks,  if data receive fromchannel one (A1)
 // If yes, store sensor value member of structure in temporary LDR variable
if(currentPinRead.pin==1)
      {
      LDR = map(currentPinRead.value, 0, 1023, 0, 255); //map ADC1 value to light value
// dispay light dependent resistor output in first line of 16x2 LCD  
      lcd.setCursor(9, 0);
      lcd.print("LIGHT = ");
      lcd.setCursor(7, 1);
      lcd.print(LDR);
      lcd.print("LUX");


      }  
      if(currentPinRead.pin==3)
      {
      LDR = map(currentPinRead.value, 0, 1023, 0, 255); //map ADC1 value to light value
// dispay light dependent resistor output in first line of 16x2 LCD  
      lcd.setCursor(9, 1);
      lcd.print("LIGHT = ");
      lcd.setCursor(7, 1);
      lcd.print(LDR);
      lcd.print("LUX");


      }  




      }




      taskYIELD(); // terminate the task and inform schulder about it




    }




  }




uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
lcd1:VSS
lcd1:VDD
lcd1:V0
lcd1:RS
lcd1:RW
lcd1:E
lcd1:D0
lcd1:D1
lcd1:D2
lcd1:D3
lcd1:D4
lcd1:D5
lcd1:D6
lcd1:D7
lcd1:A
lcd1:K