#include <Arduino_FreeRTOS.h>
#include <semphr.h>
#include <DHT22.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DHTPIN 5
SemaphoreHandle_t sMutex;
DHT22 dht(DHTPIN);
LiquidCrystal_I2C lcd(0x27, 20, 4);
int temp;
int temp_u;
int hume;
int hum_s;
int hume_s;
void sensorH(void* pvParameters){
while(true){
pinMode(A0, INPUT);
temp = dht.getTemperature();
hume = dht.getHumidity();
hum_s = analogRead(A0);
hume_s = map(hum_s, 0, 1023, 100, 0);
if(xSemaphoreTake(sMutex, 10)==pdPASS){
if(temp <= 10){
lcd.setCursor(0, 0);
lcd.print("Temp. baja: ");
lcd.print(temp);
lcd.print(" ");
temp_u = temp;
} else if (temp >= 18){
lcd.setCursor(0, 0);
lcd.print("Temp. alta: ");
lcd.print(temp);
lcd.print(" ");
temp_u = temp;
}else if(temp >= 10 && temp <= 18){
lcd.setCursor(0,0);
lcd.print("Temp.: ");
lcd.print(temp);
lcd.print(" ");
temp_u = temp;
}
if(hume <= 39){
lcd.setCursor(0, 1);
lcd.print("Hum. A. baja: ");
lcd.print(hume);
lcd.print("%");
lcd.print(" ");
} else if(hume >= 60){
lcd.setCursor(0,1);
lcd.print("Hum. A. alta: ");
lcd.print(hume);
lcd.print("%");
lcd.print(" ");
}else if(hume >= 40 && hume <= 60){
lcd.setCursor(0,1);
lcd.print("Hum. A.: ");
lcd.print(hume);
lcd.print("%");
lcd.print(" ");
}
if(hume_s < 40){
lcd.setCursor(0,2);
lcd.print("Hum. S. baja: ");
lcd.print(hume_s);
lcd.print("%");
lcd.print(" ");
}else if(hume_s > 60){
lcd.setCursor(0,2);
lcd.print("Hum. S. alta: ");
lcd.print(hume_s);
lcd.print("%");
lcd.print(" ");
}else if(hume_s >= 40 && hume_s <= 60){
lcd.setCursor(0,2);
lcd.print("Hum. S.: ");
lcd.print(hume_s);
lcd.print("%");
lcd.print(" ");
}
xSemaphoreGive(sMutex);
}
vTaskDelay(338);
}
}
void bomba(void* pvParameters){
while(true){
if(xSemaphoreTake(sMutex, 10)== pdPASS){
pinMode(4, OUTPUT);
if(hume_s < 40){
digitalWrite(4, HIGH);
xSemaphoreGive(sMutex);
} else if(hume_s >= 40){
digitalWrite(4, LOW);
xSemaphoreGive(sMutex);
}
vTaskDelay(100);
}
}
}
void setup() {
Serial.begin(9600);
pinMode(5, INPUT);
lcd.init();
lcd.backlight();
sMutex = xSemaphoreCreateMutex();
while(sMutex == NULL){}
xTaskCreate(sensorH, "SENSOR", 128, NULL, 1, NULL);
xTaskCreate(bomba, "BOMBA", 128, NULL, 1, NULL);
vTaskStartScheduler();
}
void loop() {
}