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

#define PIN_RECEIVER 16
#define DHT22_Pin 18

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

int Li = 20;
int Lii = 0;

bool isConfiguring = false;
int configValue = 24;  // Valor inicial da temperatura configurável

void setup() {
  // put your setup code here, to run once:
  lcd.begin(20, 4);
  receiver.enableIRIn();
}

void loop() {
  // Checks received an IR signal
  if (receiver.decode()) {
    translateIR();
    receiver.resume();  // Receive the next value
  }

  if (!isConfiguring) {
    lcd.setCursor(2, 0);
    lcd.print("Controle de temp.");
    lcd.setCursor(2, 1);
    lcd.print("Temp atual: ");
    lcd.print(configValue);
    lcd.print(" C");
    lcd.setCursor(1, 2);
    lcd.print("Humidade: ");
    lcd.setCursor(0, 3);
    lcd.print(Scroll_LCD_Left("Aperte [MENU] para configurar"));
    delay(250);
  }
}

String Scroll_LCD_Left(String StrDisplay) {
  String resultado;
  String StrProcess = "                " + StrDisplay + "                ";
  resultado = StrProcess.substring(Li, Lii);
  Li++;
  Lii++;
  if (Li > StrProcess.length()) {
    Li = 20;
    Lii = 0;
  }
  return resultado;
}

void lcdPrint(char* text) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("button pressed:");
  lcd.setCursor(0, 1);
  lcd.print(text);
  lcd.print(" code: ");
  lcd.print(receiver.decodedIRData.command);
}

void translateIR() {
  switch (receiver.decodedIRData.command) {
    case 226: // MENU
      isConfiguring = true;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Config. Temp:");
      lcd.setCursor(0, 1);
      lcd.print("Temp: ");
      lcd.print(configValue);
      lcd.print(" C");
      break;
    case 2: // MAIS
      if (isConfiguring) {
        configValue++;
        updateConfigDisplay();
      }
      break;
    case 152: // MENOS
      if (isConfiguring) {
        configValue--;
        updateConfigDisplay();
      }
      break;
    case 162: // POWER (sair config)
      if (isConfiguring) {
        isConfiguring = false;
        lcd.clear();
      }
      break;
    default:
      if (!isConfiguring) {
        lcdPrint("UNKNOWN");
      }
      break;
  }
}

void updateConfigDisplay() {
  lcd.setCursor(6, 1);
  lcd.print("     "); // Limpa o valor antigo
  lcd.setCursor(6, 1);
  lcd.print(configValue);
  lcd.print(" C");
}
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT