#include <Arduino_FreeRTOS.h>
#include <queue.h>
#include <LiquidCrystal.h>
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd (7,6,5,4,3,2); //RS, E, D4, D5, D6, D7
QueueHandle_t structQueue;
struct readings {
int pin; // analog channel indentifier
int value; // sensor output value
};
void setup() {
// Serial.begin(9600);
lcd.begin(16, 2);
dht.begin();
structQueue = xQueueCreate(10, sizeof(struct readings));
if (structQueue != NULL) { //enquiry for space availability
xTaskCreate(LCD,"Receiver", 128, NULL, 2, NULL);
xTaskCreate(DHT,"Sender1",128, NULL, 1, NULL);
xTaskCreate(PotMeter,"Sender2",128, NULL, 1,NULL);
xTaskCreate(LDR,"Sender3",128,NULL,1,NULL);
}
vTaskStartScheduler();
}
void loop() {
}
void DHT(void *pvParameters){
(void) pvParameters;
for (;;){
struct readings inputData;
inputData.pin = 0;
inputData.value = digitalRead(8);
// float temperature = dht.readTemperature();
// float humidity = dht.readHumidity();
xQueueSend(structQueue, &inputData, portMAX_DELAY);
// Serial.println("DHT read");
taskYIELD();
}
}
void PotMeter(void *pvParameters){
for (;;){
struct readings inputData;
inputData.pin = 1;
inputData.value = analogRead(A0);
xQueueSend(structQueue, &inputData, portMAX_DELAY);
// Serial.println("PotMeter read");
taskYIELD();
}
}
void LDR(void *pvParameters){
for (;;){
struct readings inputData;
inputData.pin = 2;
inputData.value = analogRead(A1);
xQueueSend(structQueue, &inputData, portMAX_DELAY);
// Serial.println("LDR read");
taskYIELD();
}
}
void LCD(void * pvParameters){
(void) pvParameters;
for (;;) {
int TEMP=0;
int LDR=0;
int POT=0;
struct readings inputData;
if (xQueueReceive(structQueue, &inputData, portMAX_DELAY) == pdPASS) {
// Serial.print("Pin: ");
// Serial.print(inputData.pin);
// Serial.print(" Value: ");
// Serial.println(inputData.value);
if(inputData.pin==0) {
TEMP = dht.readTemperature();
// TEMP = (inputData.value * 500)/1024;
lcd.setCursor(0, 0);
lcd.print("T = ");
lcd.setCursor(3, 0);
lcd.print(TEMP);
lcd.print("'C");
}
if(inputData.pin==1){
POT = inputData.value;
lcd.setCursor(11,0);
lcd.print("P = ");
lcd.setCursor(14, 0);
lcd.print(POT);
lcd.print("V");
}
if(inputData.pin==2) {
LDR = map(inputData.value, 0, 1023, 0, 255);
lcd.setCursor(0, 1);
lcd.print("LIGHT = ");
lcd.setCursor(7, 1);
lcd.print(LDR);
lcd.print("LUX");
}
}
taskYIELD(); // terminate the task and inform schulder about it
}
}