/* KY-040 Rotary Encoder Counter

   Rotate clockwise to count up, counterclockwise to counter done.

   Press to reset the counter.

   Copyright (C) 2021, Uri Shaked
*/

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);

#define ENCODER_CLK 2
#define ENCODER_DT  3
#define ENCODER_SW  4

int counter = 0 ;
bool  choice = false ;

void setup() {
  // Initialize LCD
  lcd.init();
  lcd.backlight();

  // Initialize encoder pins
  pinMode(ENCODER_CLK, INPUT);
  pinMode(ENCODER_DT, INPUT);
  pinMode(ENCODER_SW, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), readEncoder, FALLING);
  menu ();
}

void readEncoder() {
  int dtValue = digitalRead(ENCODER_DT);
  if (dtValue == HIGH) {
    counter++; // Clockwise
  }
  if (dtValue == LOW) {
    counter--; // Counterclockwise
  }
}

// Get the counter value, disabling interrupts.
// This make sure readEncoder() doesn't change the value
// while we're reading it.
int getCounter() {
  int result;
  noInterrupts();
  result = counter;
  interrupts();
  return result;
}

void resetCounter() {
  noInterrupts();
  counter = 0; // Reinicia o contador para 0
  choice = false; // Reinicia o estado da escolha
  interrupts();
}


void menu ()
{
  if (choice == false)
  {
    lcd.setCursor(0, 0);
    lcd.print("      M E N U       ");
    lcd.setCursor(0, 1);
    lcd.print("   Fuso horario");
    lcd.setCursor(0, 2);
    lcd.print("   Alarme 1");
    lcd.setCursor(0, 3);
    lcd.print("   Alarme 2");
  }
  else if (counter == 1 && choice == true)
  {
    lcd.setCursor(0, 0);
    lcd.print("    M E N U - 1     ");
    lcd.setCursor(0, 1);
    lcd.print("      1 1           ");
    lcd.setCursor(0, 2);
    lcd.print("        1           ");  
    lcd.setCursor(0, 3);
    lcd.print("     1 1 1 1        ");
  }
  else if (counter == 2 && choice == true)
  {
    lcd.setCursor(0, 0);
    lcd.print("    M E N U - 2     ");
    lcd.setCursor(0, 1);
    lcd.print("       2 2 2 2      ");
    lcd.setCursor(0, 2);
    lcd.print("         2          ");
    lcd.setCursor(0, 3);
    lcd.print("       2 2 2 2      ");
  }
  else if (counter == 3 && choice == true)
  {
    lcd.setCursor(0, 0);
    lcd.print("    M E N U - 3     ");
    lcd.setCursor(0, 1);
    lcd.print("      3 3 3 3       ");
    lcd.setCursor(0, 2);
    lcd.print("        3 3 3       ");
    lcd.setCursor(0, 3);
    lcd.print("      3 3 3 3       ");
  }
}

void loop() {
  if (digitalRead(ENCODER_SW) == LOW) {
    resetCounter();
    menu () ;
  }
  if (counter == 1 && choice == false) {
    menu () ;
    lcd.setCursor(0, 1);
    lcd.print(">>");
  }
  else if (counter == 2 && choice == false) {
    menu () ;
    lcd.setCursor(0, 2);
    lcd.print(">>");
  }
  else  if (counter == 3 && choice == false) {
    menu () ;
    lcd.setCursor(0, 3);
    lcd.print(">>");
  }
  else if (counter == 4)
  {
    counter = 1 ;
    menu () ;
  }
  else if (counter <= 0 )
  {
    counter = 3 ;
    menu () ;
  }  
  else if (counter == 0 )
  {
    menu () ;
  }  
  delay(50);
}