#include <LiquidCrystal.h>
#include <IRremote.h>

LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
IRrecv receiver(25);

const float BETA = 3950;
const int ts = A0;
const int hs = A1;

const int heaterl = 6;
const int trl = 5;
const int tgl = 4;
const int tbl = 3;
const int tol = 2;

const int humidityl = 27;
const int hrl = 45;
const int hgl = 41;
const int hbl = 37;

String command = "";

String Mode = "Day mode"; // Az "Mode" változót String típusúra változtattam

bool lcdContent = false;

uint8_t dot[] = {
  0b00000,
  0b00000,
  0b01110,
  0b01110,
  0b01110,
  0b00000,
  0b00000,
  0b00000
};

uint8_t degree[] = {
  0b11100,
  0b10100,
  0b11100,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000
};

uint8_t percentage[] = {
  0b00010,
  0b00010,
  0b10100,
  0b00100,
  0b01000,
  0b01001,
  0b10000,
  0b10000
};

void setup() {
  lcd.createChar(1, dot);
  lcd.createChar(2, degree);
  lcd.createChar(3, percentage);
  receiver.enableIRIn();
  lcd.begin(20, 4);
  pinMode(ts, INPUT);
  pinMode(hs, INPUT);
  pinMode(heaterl, OUTPUT);
  pinMode(humidityl, OUTPUT);
  pinMode(trl, OUTPUT);
  pinMode(tgl, OUTPUT);
  pinMode(tbl, OUTPUT);
  pinMode(tol, OUTPUT);
  pinMode(hrl, OUTPUT);
  pinMode(hgl, OUTPUT);
  pinMode(tbl, OUTPUT);
  lcd.setCursor(6,1);
  lcd.print("Booting");
  delay(1000);
  lcd.clear();
  //boot_test();
  delay(1000);
  lcd.clear();
  lcd.setCursor(5,1);
  lcd.print("Measuring");
  delay(1000);
  lcd.clear();
}

void translateIR() {
  switch (receiver.decodedIRData.command) {
    case 226: // "Menu" gomb
      Menu();
      break;
    case 162: // "Power" gomb
      SwitchMode();
      break;
  }
}

void loop() {
  int analogValue = analogRead(ts);
  float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
  float humidity = analogRead(hs);
  humidity = humidity / 10;

  if (receiver.decode()) {
    translateIR();
    receiver.resume();
  }
  if (Mode == "Day mode") {
    lcd.setCursor(0, 0);
    lcd.print(celsius);
    lcd.print("\2C, ");
    lcd.print(humidity);
    lcd.print("\3");
    if (celsius > 15 && celsius < 20) {
      digitalWrite(tgl, HIGH);
    } else {
      digitalWrite(tgl, LOW);
    }
    if (celsius > 25) {
      digitalWrite(trl, HIGH);

    } else {
      digitalWrite(trl, LOW);
    }
    if (celsius < 10) {
      digitalWrite(tbl, HIGH);
      analogWrite(heaterl, HIGH);
    } else {
      digitalWrite(tbl, LOW);
    }
    if ((celsius > 10 && celsius < 15) || (celsius > 20 && celsius < 25)){
      digitalWrite(tol, HIGH);
    } else {
      digitalWrite(tol, LOW);
    }
    
    if (humidity > 80 && humidity < 90) {
      digitalWrite(hgl, HIGH);
    } else {
      digitalWrite(hgl, LOW);
    }
    if (humidity > 90) {
      digitalWrite(hrl, HIGH);
    } else {
      digitalWrite(hrl, LOW);
    }
    if (humidity < 80) {
      digitalWrite(hbl, HIGH);
    } else {
      digitalWrite(hbl, LOW);
    }
    delay(1000);
  } 
    else if (Mode == "Night mode") { // "else if" használata
      lcd.setCursor(0, 0);
      lcd.print(celsius);
      lcd.print("\2C, ");
      lcd.print(humidity);
      lcd.print("\3");
    if (celsius > 15 && celsius < 20) {
      analogWrite(tgl, 100);
    } else {
      analogWrite(tgl, 0);
    }
    if (celsius > 25) {
      analogWrite(trl, 100);

    } else {
      analogWrite(trl, 0);
    }
    if (celsius < 10) {
      analogWrite(tbl, 100);
      analogWrite(heaterl, 100);
    } else {
      analogWrite(tbl, 0);
    }
    if ((celsius > 10 && celsius < 15) || (celsius > 20 && celsius < 25)){
      analogWrite(tol, 100);
    } else {
      analogWrite(tol, 0);
    }
    
    if (humidity > 80 && humidity < 90) {
      analogWrite(hgl, 100);
    } else {
      analogWrite(hgl, 0);
    }
    if (humidity > 90) {
      analogWrite(hrl, 100);
    } else {
      analogWrite(hrl, 0);
    }
    if (humidity < 80) {
      analogWrite(hbl, 100);
      analogWrite(humidityl, 100);
    } else {
      analogWrite(hbl, 0);
    }
    delay(1000);
  }
}

void Menu() {
  lcd.clear();
  lcd.setCursor(8, 0);
  lcd.print("MENU");
  while (true) {
    T_IR_F_M();
  }
}

void T_IR_F_M(){
  switch (receiver.decodedIRData.command) {
      case 2:
        lcd.clear();
        command = "Up";
        lcd.setCursor(0, 1);
        lcd.print("Up");
        break;
      case 168:
        lcd.clear();
        command = "OK";
        lcd.setCursor(0, 1);
        lcd.print("Up");
        break;
      case 152:
        lcd.clear();
        command = "Down";
        lcd.setCursor(0, 1);
        lcd.print("Up");
        break;
    }
  receiver.resume();
}

void SwitchMode() {
  if (Mode == "Day mode") {
    Mode = "Night mode";
    lcd.clear();
    lcd.setCursor(4, 1);
    lcd.print("Night mode");
    delay(1000);
    lcd.clear();
  } else if (Mode == "Night mode") { // "else if" használata
    Mode = "Day mode";
    lcd.clear();
    lcd.setCursor(5, 1);
    lcd.print("Day mode");
    delay(1000);
    lcd.clear();
  }
}

void boot_test() {
  lcd.setCursor(0,0);
  lcd.print("Temperature LED test");
  digitalWrite(trl, LOW);
  digitalWrite(tgl, LOW);
  digitalWrite(tbl, LOW);
  delay(100);
  digitalWrite(trl, HIGH);
  delay(1000);
  digitalWrite(tgl, HIGH);
  delay(1000);
  digitalWrite(tbl, HIGH);
  delay(1000);
  digitalWrite(tol, HIGH);
  delay(1000);
  digitalWrite(trl, LOW);
  delay(1000);
  digitalWrite(tgl, LOW);
  delay(1000);
  digitalWrite(tbl, LOW);
  delay(1000);
  digitalWrite(tol, LOW);
  delay(1000);
  digitalWrite(trl, HIGH);
  digitalWrite(tgl, HIGH);
  digitalWrite(tbl, HIGH);
  digitalWrite(tol, HIGH);
  delay(500);
  digitalWrite(trl, LOW);
  digitalWrite(tgl, LOW);
  digitalWrite(tbl, LOW);
  digitalWrite(tol, LOW);
  delay(500);
  digitalWrite(trl, HIGH);
  digitalWrite(tgl, HIGH);
  digitalWrite(tbl, HIGH);
  digitalWrite(tol, HIGH);
  delay(500);
  digitalWrite(trl, LOW);
  digitalWrite(tgl, LOW);
  digitalWrite(tbl, LOW);
  digitalWrite(tol, LOW);
  delay(500);
  digitalWrite(trl, HIGH);
  digitalWrite(tgl, HIGH);
  digitalWrite(tbl, HIGH);
  digitalWrite(tol, HIGH);
  delay(500);
  digitalWrite(trl, LOW);
  digitalWrite(tgl, LOW);
  digitalWrite(tbl, LOW);
  digitalWrite(tol, LOW);
  delay(1000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Heater led test");
  delay(1000);
  digitalWrite(heaterl, HIGH);
  delay(1000);
  digitalWrite(heaterl, LOW);
  delay(1000);
  lcd.clear();

  lcd.setCursor(0,0);
  lcd.print("Humidity LED test");
  digitalWrite(hrl, LOW);
  digitalWrite(hgl, LOW);
  digitalWrite(hbl, LOW);
  delay(100);
  digitalWrite(hrl, HIGH);
  delay(1000);
  digitalWrite(hgl, HIGH);
  delay(1000);
  digitalWrite(hbl, HIGH);
  delay(1000);
  digitalWrite(hrl, LOW);
  delay(1000);
  digitalWrite(hgl, LOW);
  delay(1000);
  digitalWrite(hbl, LOW);
  delay(1000);
  digitalWrite(hrl, HIGH);
  digitalWrite(hgl, HIGH);
  digitalWrite(hbl, HIGH);
  delay(500);
  digitalWrite(hrl, LOW);
  digitalWrite(hgl, LOW);
  digitalWrite(hbl, LOW);
  delay(500);
  digitalWrite(hrl, HIGH);
  digitalWrite(hgl, HIGH);
  digitalWrite(hbl, HIGH);
  delay(500);
  digitalWrite(hrl, LOW);
  digitalWrite(hgl, LOW);
  digitalWrite(hbl, LOW);
  delay(500);
  digitalWrite(hrl, HIGH);
  digitalWrite(hgl, HIGH);
  digitalWrite(hbl, HIGH);
  delay(500);
  digitalWrite(hrl, LOW);
  digitalWrite(hgl, LOW);
  digitalWrite(hbl, LOW);
  delay(1000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Humidifier led test");
  delay(1000);
  digitalWrite(humidityl, HIGH);
  delay(1000);
  digitalWrite(humidityl, LOW);
  delay(1000);
  lcd.clear();
}