#include <Arduino_FreeRTOS.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd (7,6,5,4,3,2);//RS,E,D4,D5,D6,D7
#include<queue.h>
//define a struct
struct pinRead
{
int pin;//analog channel identifier
int value;//sensor output value
};
//This lines defines the handler for struct Queue to access it for tasks reference
QueueHandle_t structQueue;
//These two function initializes LCD and UART communication libreary
void setup()
{
Serial.begin(9600);
lcd.begin(16,2);//ENable LCD library
//create Structure Queue
structQueue= xQueueCreate(10,//Queue length
sizeof(struct pinRead)//Queue item size
);
if(structQueue!=NULL)
{
//create task that consumes the queue if it was created
xTaskCreate(TaskLCD,//Taskfunction
"Displaydata",//A name just for humnas
128,//This stack size can be checked & adjusted by reading the stack highwater
NULL,2,//Priority with 3(configMAX_PRIORITIES -1)being the highest, and 0 being the lowest
NULL
);
//Create task that publish data in the queue if it was created.
xTaskCreate(TaskTempReadPin0,//Task function
"AnalogReadPin0",//Task namd=e
128,//stack size
NULL,1,//Priority
NULL);
xTaskCreate(TaskLightReadPin1,//Task function
"AnalogReadPin1",//Task namd=e
128,//stack size
NULL,1,//Priority
NULL);
}
vTaskStartScheduler();
}
void loop() {
// put your main code here, to run repeatedly:
}
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 in value element of struct
xQueueSend(structQueue,¤tPinRead,portMAX_DELAY);//write struct message to queue
Serial.println("Channel_0");
taskYIELD();//terminate the task and inform schudler about it
}
}
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,¤tPinRead,portMAX_DELAY);
//write struct message in queue
Serial.println("Channel_1");//print Channel_1 on serial monitor
taskYIELD();//terminate the task and inform schelduer 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 received data
//Read structure element from queue and check if data received successfully
if(xQueueReceive(structQueue,¤tPinRead,portMAX_DELAY)==pdPASS)
{
Serial.print("Pin: ");
Serial.print(currentPinRead.pin);
Serial.print("Value: ");
Serial.println(currentPinRead.value);
//if condition checks if data receive from channel zero
//if, yes store sensor value member of structure in temporeary temp variable
if(currentPinRead.pin==0)
{
TEMP =(currentPinRead.value *500)/1024;//convert adc value into temperature
///display
lcd.setCursor(0,0);
lcd.print("Temp= ");
lcd.setCursor(7,0);
lcd.print(TEMP);
lcd.print("C");
}
//if condition checks if data receive from channel one A1
//if yes, store sensor value member of structure in temporeary LDR variable
if(currentPinRead.pin==1)
{
LDR=map(currentPinRead.value,0,1023,0,255);//map ADC1 value to light value
//display light dependent resistor output in first line of 16x2 LCD
lcd.setCursor(0,1);
lcd.print("LIGHT=");
lcd.setCursor(7,1);
lcd.print(LDR);
lcd.print("LUX");
}
}
taskYIELD();//terminate the task and inform schuler about it
}
}