#define ntc 35
#define LDR_A 4
#define LDR_D 2
#define air 27
#define lamp 18
int ana_val;
int LDR_Aval;
int LDR_Dval;
int air_status = 0;
int lamp_status = 0;
const float BETA = 3950;
const float GAMMA = 0.7;
const float RL10 = 50;


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


void setup() {
  pinMode(ntc, INPUT);
  pinMode(LDR_A, INPUT);
  pinMode(LDR_D, INPUT_PULLUP);
  pinMode(air, OUTPUT);
  pinMode(lamp, OUTPUT);
  Serial.begin(9600);

  lcd.init();
  lcd.backlight();

}


void loop() {

  ana_val = analogRead(ntc);
  float celsius = 1 / (log(1 / (4095. / ana_val - 1)) / BETA + 1.0 / 298.15) - 273.15;

  LDR_Aval = analogRead(LDR_A);
  float voltage = LDR_Aval / 4095. * 5;
  float resistance = 2000 * voltage / (1 - voltage / 5.0);
  float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));

  LDR_Dval = digitalRead(LDR_D);


  if (ana_val <= 1825) {
    air_status = 1;
    lcd.setCursor(5, 3);
    lcd.print("ON ");

  }
  if (ana_val >= 1913) {
    air_status = 0;
    lcd.setCursor(5, 3);
    lcd.print("OFF");

  }
  if (LDR_Aval >= 2531) {
    lamp_status = 1;
    lcd.setCursor(17, 3);
    lcd.print("ON ");

  }
  if (LDR_Aval < 2531) {
    lamp_status = 0;
    lcd.setCursor(17, 3);
    lcd.print("OFF");

  }
  digitalWrite(air, air_status);
  digitalWrite(lamp, lamp_status);
  
  lcd.setCursor(0, 0);
  lcd.print("  Temp  |   Light  ");
  lcd.setCursor(0, 1);
  lcd.print(celsius);
  lcd.setCursor(6, 1);
  lcd.print("C");
  lcd.setCursor(8, 1);
  lcd.print("|");
  lcd.setCursor(10, 1);
  lcd.print(lux);
  lcd.setCursor(17, 1);
  lcd.print("lux");
  lcd.setCursor(8, 2);
  lcd.print("|");
  lcd.setCursor(0, 3);
  lcd.print("Air> ");
  lcd.setCursor(8, 3);
  lcd.print("|  Lamp> ");


}