#include "DHT.h";
#include <LiquidCrystal_I2C.h>
#include <Preferences.h>
Preferences memory;
// LCD
LiquidCrystal_I2C lcd(0x27,16,2);
// RELAY
#define LAMPPIN 16
// DHT SENSOR
#define DHTPIN 4
#define DHTTYPE DHT22
float TEMPERATURA;
float HUMEDAD;
DHT sensor(DHTPIN, DHTTYPE);
// BUTTONS
const int MAIN_BUTTON = 14;
const int SUM_BUTTON = 27;
const int REST_BUTTON = 26;
int state;
int menu = 0;
int lastMenu = 0;
float lastWishedTemp = 35.5;
float wishedTemp = 36;
int lastLampMode = 0;
int lampMode = 0;
bool lampStatus = true;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
sensor.begin();
memory.begin("config", false);
wishedTemp = memory.getFloat("wishedTemp", 36);
lampMode = memory.getInt("lampMode", 0);
pinMode(MAIN_BUTTON, INPUT_PULLUP);
pinMode(SUM_BUTTON, INPUT_PULLUP);
pinMode(REST_BUTTON, INPUT_PULLUP);
pinMode(LAMPPIN, OUTPUT);
lcd.init();
lcd.clear();
lcd.backlight();
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
TEMPERATURA = sensor.readTemperature();
HUMEDAD = sensor.readHumidity();
if (lampStatus) digitalWrite(LAMPPIN, HIGH);
if (!lampStatus) digitalWrite(LAMPPIN, LOW);
if (menu == 0) {
lcd.setCursor(0,0); //Set cursor to character 2 on line 0
lcd.print("Temp: ");
lcd.setCursor(6,0); //Move cursor to character 2 on line 1
lcd.print(TEMPERATURA);
lcd.setCursor(12,0);
lcd.print("C");
lcd.setCursor(0,1); //Set cursor to character 2 on line 0
lcd.print("Hum: ");
lcd.setCursor(6,1); //Move cursor to character 2 on line 1
lcd.print(HUMEDAD);
lcd.setCursor(12,1);
lcd.print("%");
if (digitalRead(SUM_BUTTON) == LOW) {
menu = menu + 1;
delay(500);
}
}
if (menu == 1) {
lcd.setCursor(1,0); //Set cursor to character 2 on line 0
lcd.print("Temp deseada: ");
lcd.setCursor(4,1); //Set cursor to character 2 on line 0
lcd.print(wishedTemp);
if (digitalRead(SUM_BUTTON) == LOW) {
menu = menu + 1;
}
if (digitalRead(REST_BUTTON) == LOW) {
menu = menu - 1;
}
if (digitalRead(MAIN_BUTTON) == LOW) {
menu = 10;
}
}
if (menu == 10) {
lcd.setCursor(0,0); //Set cursor to character 2 on line 0
lcd.print("Seleccionar temp: ");
lcd.setCursor(7,1); //Set cursor to character 2 on line 0
lcd.print(wishedTemp);
if (digitalRead(SUM_BUTTON) == LOW) {
wishedTemp = wishedTemp + 0.5;
}
if (digitalRead(REST_BUTTON) == LOW) {
wishedTemp = wishedTemp - 0.5;
}
if (digitalRead(MAIN_BUTTON) == LOW) {
memory.putFloat("wishedTemp", wishedTemp);
menu = 1;
}
}
if (menu == 2) {
lcd.setCursor(0,0); //Set cursor to character 2 on line 0
lcd.print("Lampara modo: ");
lcd.setCursor(2,1); //Set cursor to character 2 on line 0
if (lampMode == 0) lcd.print("Automatico");
if (lampMode == 1) lcd.print("Prendida");
if (lampMode == 2) lcd.print("Apagada");
if (digitalRead(REST_BUTTON) == LOW) {
menu = menu - 1;
}
if (digitalRead(MAIN_BUTTON) == LOW) {
menu = 20;
}
}
if (menu == 20) {
if (digitalRead(SUM_BUTTON) == LOW) {
lampMode = lampMode < 2 ? lampMode + 1 : lampMode;
}
if (digitalRead(REST_BUTTON) == LOW) {
lampMode = lampMode > 0 ? lampMode - 1 : lampMode;
}
lcd.setCursor(0,0); //Set cursor to character 2 on line 0
lcd.print("Seleccionar modo: ");
lcd.setCursor(2,1); //Set cursor to character 2 on line 0
if (lampMode == 0) lcd.print("Automatico");
if (lampMode == 1) lcd.print("Prendida");
if (lampMode == 2) lcd.print("Apagada");
if (digitalRead(MAIN_BUTTON) == LOW) {
memory.putInt("lampMode", lampMode);
menu = 2;
}
}
bool sameMenu = lastMenu == menu;
bool sameLampMode = lastLampMode == lampMode;
bool sameWishedTemp = lastWishedTemp == wishedTemp;
if (!sameMenu || !sameLampMode || !sameWishedTemp) {
lcd.clear();
lastMenu = menu;
lastLampMode = lampMode;
lastWishedTemp = wishedTemp;
}
if (lampMode == 0) {
if (lampStatus && TEMPERATURA >= wishedTemp) {
lampStatus = false;
}
if (!lampStatus && TEMPERATURA <= wishedTemp - 0.5) {
lampStatus = true;
}
}
if (lampMode == 1) lampStatus = true;
if (lampMode == 2) lampStatus = false;
}