#include <Wire.h>
#include <LiquidCrystal_I2C.h>

volatile unsigned long screenTime;
volatile int LastCurentMenuFlag;
volatile int CurentMenuFlag;
String line1Text;
String line2Text;
String line3Text;
String line4Text;
volatile int contro;
volatile boolean LcdClearFlag;
volatile unsigned int i;
volatile unsigned int subMenuValueFlag;
volatile int menucount;
volatile int menu;
volatile int clkValue;
volatile int dtValue;
volatile int counter;
volatile int dir;
volatile int MIN;
volatile int MAX;
volatile int menuflag;
String LcdText[]={"", "", "", ""};

String DefautMenu[]={" HE THONG GIAM SAT", "NHIET DO:", "AP SUAT:", "DO AM:"};

String MainMenu[]={"Toc do", "Ap suat", "Nhiet do", "Do am"};

LiquidCrystal_I2C mylcd(0x27,20,4);
int subMenuValue[4][4] = {
  {0, 0, 0, 0},
  {0, 0, 0, 0},
  {0, 0, 0, 0},
  {0, 0, 0, 0}
};
String subMenu[4][4] = {
  {"Toc do banh rang", "Toc do dai", "Toc do tua", "Quay lai"},
  {"Ap suat dau", "Ap suat xang", "Ap suat ga", "Quay lai"},
  {"Nhiet do nuoc", "Nhiet do xang", "Nhiet do dau", "Quay lai"},
  {"Do am diem 1", "Do am diem 2", "Do am diem 3", "Quay lai"}
};

void updateSubMenuValue() {
  if (CurentMenuFlag > 10) {
    subMenuValue[LastCurentMenuFlag-1][contro-1] = (constrain(subMenuValue[LastCurentMenuFlag-1][contro-1] + dir, 0, 100));

  }
}

void updateContro() {
  if (0 <= CurentMenuFlag && CurentMenuFlag <= 4) {
    contro = constrain(contro + dir, 1, 4);

  }
}

void checkEncoder() {
  clkValue = digitalRead(2);
  delayMicroseconds(1000);
  dtValue = digitalRead(4);
  if (clkValue != dtValue) {
    dir = 1;

  } else {
    dir = -1;

  }
}

void attachInterrupt_fun_FALLING_2() {
  checkEncoder();
  updateContro();
  updateSubMenuValue();
  screenTime = millis();
}

void attachInterrupt_fun_FALLING_3() {
  if (digitalRead(3) == LOW) {
    LcdClearFlag = true;
    screenTime = millis();
    if (CurentMenuFlag == -1) {
      LastCurentMenuFlag = CurentMenuFlag;
      CurentMenuFlag = 0;

    } else {
      if (contro == 4 && CurentMenuFlag != 0) {
        LastCurentMenuFlag = CurentMenuFlag;
        CurentMenuFlag = 0;
        contro = 1;

      } else if (CurentMenuFlag == 0) {
        LastCurentMenuFlag = CurentMenuFlag;
        CurentMenuFlag = contro;
        contro = 1;
      } else if (contro != 4 && (CurentMenuFlag <= 4 && CurentMenuFlag >= 1)) {
        LastCurentMenuFlag = CurentMenuFlag;
        CurentMenuFlag = String(String(CurentMenuFlag) + String(contro)).toInt();
      } else if (CurentMenuFlag > 10) {
        CurentMenuFlag = LastCurentMenuFlag;
      }

    }

  }
}

void updateLCD() {
  //Update lcd display text
  switch (CurentMenuFlag) {
   case -1:
    for (int i = 0; i <= 3; i = i + (1)) {
      LcdText[i] = DefautMenu[i];
    }
    break;
   case 0:
    for (int i = 0; i <= 3; i = i + (1)) {
      LcdText[i] = MainMenu[i];
    }
    break;
  }
  if (CurentMenuFlag <= 4 && CurentMenuFlag >= 1) {
    for (int i = 0; i <= 3; i = i + (1)) {
      LcdText[i] = subMenu[CurentMenuFlag-1][i];
    }

  } else if (CurentMenuFlag > 10) {
    LcdText[0] = subMenu[LastCurentMenuFlag-1][contro-1];
    LcdText[1] = String("        ") + String(subMenuValue[LastCurentMenuFlag-1][contro-1]) + String("   ");
    LcdText[2] = "";
    LcdText[3] = "";
  }
}

void MenuDisplay(String line1, String line2, String line3, String line4, int vitri) {
  if (LcdClearFlag != false && LastCurentMenuFlag != CurentMenuFlag) {
    mylcd.clear();
    LcdClearFlag = false;

  }
  if (CurentMenuFlag != -1 && CurentMenuFlag <= 4) {
    for (int i = 1; i <= 4; i = i + (1)) {
      mylcd.setCursor(20-1, i-1);
      mylcd.print('~');
      if (vitri == i) {
        mylcd.setCursor(1-1, i-1);
        mylcd.print('>');

      } else {
        mylcd.setCursor(1-1, i-1);
        mylcd.print(' ');

      }
    }

  }
  mylcd.setCursor(2-1, 1-1);
  mylcd.print(line1);
  mylcd.setCursor(2-1, 2-1);
  mylcd.print(line2);
  mylcd.setCursor(2-1, 3-1);
  mylcd.print(line3);
  mylcd.setCursor(2-1, 4-1);
  mylcd.print(line4);
}

void setup(){
  screenTime = 0;
  LastCurentMenuFlag = 0;
  CurentMenuFlag = -1;
  line1Text = "";
  line2Text = "";
  line3Text = "";
  line4Text = "";
  contro = 1;
  LcdClearFlag = true;
  i = 0;
  subMenuValueFlag = 0;
  menucount = 3;
  menu = -1;
  clkValue = 0;
  dtValue = 0;
  counter = 0;
  dir = 0;
  MIN = 0;
  MAX = 100;
  menuflag = 0;
  pinMode(2, INPUT_PULLUP);
  pinMode(4, INPUT);
  mylcd.init();
  mylcd.backlight();
  pinMode(3, INPUT);
  Serial.begin(9600);
}

void loop(){
  attachInterrupt(digitalPinToInterrupt(2),attachInterrupt_fun_FALLING_2,FALLING);

  //Switch pressed
  attachInterrupt(digitalPinToInterrupt(3),attachInterrupt_fun_FALLING_3,FALLING);

  //Main loop
  updateLCD();
  MenuDisplay(LcdText[0], LcdText[1], LcdText[2], LcdText[3], contro);
  if (CurentMenuFlag != -1 && millis() - screenTime > 10000) {
    LastCurentMenuFlag = CurentMenuFlag;
    CurentMenuFlag = -1;
    screenTime = millis();
    LcdClearFlag = true;
    Serial.println(CurentMenuFlag);
    Serial.println((millis() - screenTime));

  }

}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
encoder1:CLK
encoder1:DT
encoder1:SW
encoder1:VCC
encoder1:GND
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND
r1:1
r1:2