#include <LiquidCrystal.h>

// LCD: RS, E, D4, D5, D6, D7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int tempPin = A1;
const int ldrPin = A0;

unsigned long previousMillis = 0;
const long interval = 5000;

byte sun[8] = {
  0b00100,
  0b10101,
  0b01110,
  0b11111,
  0b01110,
  0b10101,
  0b00100,
  0b00000
};

byte cloud[8] = {
  0b00000,
  0b01100,
  0b10110,
  0b11111,
  0b11111,
  0b01110,
  0b00000,
  0b00000
};

byte moon[8] = {
  0b00011,
  0b00110,
  0b01100,
  0b11000,
  0b11000,
  0b01100,
  0b00110,
  0b00011
};

void setup() {
  lcd.begin(16, 2);
  lcd.createChar(0, sun);
  lcd.createChar(1, cloud);
  lcd.createChar(2, moon);

  Serial.begin(9600);

  lcd.setCursor(0, 0);
  lcd.print("Ilmajaam alustab");
  lcd.setCursor(0, 1);
  lcd.print("Laen andmeid...");
  delay(3000);
  lcd.clear();
}

void loop() {
  unsigned long currentMillis = millis();
  
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    int tempRaw = analogRead(tempPin);
    float voltage = tempRaw * (5.0 / 1023.0);
    float temperature = (voltage - 0.5) * 100.0;

    int ldrValue = analogRead(ldrPin);

    Serial.print("Temp: ");
    Serial.print(temperature);
    Serial.print(" C | Valgus: ");
    Serial.println(ldrValue);

    lcd.clear();

    lcd.setCursor(0, 0);
    if (ldrValue > 935) {
      lcd.print("Paikseline");
      lcd.setCursor(13, 0);
      lcd.write(byte(0)); // ☀️
    } else if (ldrValue > 925) {
      lcd.print("Pilvine");
      lcd.setCursor(13, 0);
      lcd.write(byte(1)); // ☁️
    } else {
      lcd.print("Oö / Kuu");
      lcd.setCursor(13, 0);
      lcd.write(byte(2)); // 🌙
    }

    lcd.setCursor(0, 1);
    lcd.print("Temp: ");
    lcd.print(temperature, 1);
    lcd.print((char)223);
    lcd.print("C");
  }
}