#include <Wire.h> //include the use of SDA and SCL pins
#include <LiquidCrystal_I2C.h> //includes the i2c Library " Ada fruit is not compatible"

LiquidCrystal_I2C lcd(0x27, 16, 2); //declares my lcd name to lcd, used interface to 0x27 and size to 16*2


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); //initialize the Serial monitor
  lcd.begin(16,2); //begins the lcd
  lcd.backlight(); //turn on the back light of the LCD "turned off by default"
  lcd.clear(); //clears any old data on the lcd
  lcd.print("Temp: "); //prints on the home position (0,0)




}

void loop() {
  // put your main code here, to run repeatedly:
  float temp = analogRead(A0); //read raw data from sensor pin A0
  temp = map (temp,0,1023,1023,0); //the raw data is upside down "0 means too hot and 1023 means too cold" so i am remaping the values for 0 to be representing cold and 1023 representing hot
  Serial.print(temp); //print the raw data on the serial monitor and stay on the same line

  float Temp ; //declares a float variable
  //the following TWO lines of codes changes the raw data to equivelant temp in Kelvin
  Temp = log ( ( ( 10240000 / temp ) - 10000 ) ) ;
  Temp = 1 / ( 0.001129148 + ( 0.000234125 * Temp ) + ( 0.0000000876741 * Temp * Temp * Temp ) ) ;

  Temp = Temp - 273.15 ; //changes temp from kelvin to Celciuse 
  Temp = round(Temp);//rounding the value to the nearest 1 (can be neglected)

  float TempF; //declares a flaot variable to store temp in feh.
  TempF = (Temp * 1.80) + 32.00; //calculating the feh. temp
  TempF = round(TempF); //rounding the value to the nearest 1 (can be neglected)

  Serial.print(" ----- "); //printing on the serial monitor several dashes
  Serial.print(Temp); //printing on the serial monitor the temp in C and stay on the same line
  Serial.print (" C ----- "); //printing on the serial monitor Celc unit (C) and the some dashes and stay in the same line
  Serial.print (TempF); //printing the Temp in Feh and stays on the same line 
  Serial.println(" F"); //printing on the serial monitor the  Feh unit (F) then break to the next line
  //meanining it will print the following in one line : raw data ----- temp in Cel C ----- temp in Feh F

//printing on the LCD temp in C 
  lcd.setCursor(0, 1); //setting the cursor to 1st col and 2nd row

  lcd.print((int)Temp); //printing the Temp in C withouth the decimal numbers
  lcd.print(" C  "); //printing the Cel Unit then adding 2 spaces after it for the feh. value to be a little far away 

//printing on the LCD temp in F
  lcd.print((int)TempF);
  lcd.print(" F   ");


//adding the if condition to write either normal or danger on the LCD
if (Temp >=16 && Temp <=40){
  lcd.setCursor(6,0);
  lcd.print("Normal  "); //the 2 extra spaces is to overwrite the exclamation mark whenever danger is written then the temp goes back to normal
}
else{
  lcd.setCursor(6,0);
  lcd.print("Danger!");
}

  delay(500); //delay befor repeating and rereading form the sensor 

}