/****************************************************************************** 
BALA

This example reads from the MLX90614 and prints out ambient and object 
temperatures every half-second or so. Open the serial monitor and set the
baud rate to 9600.

Hardware Hookup (if you're not using the eval board):
MLX90614 ------------- Arduino
  VDD ------------------ 3.3V
  VSS ------------------ GND
  SDA ------------------ SDA (A4 on older boards)
  SCL ------------------ SCL (A5 on older boards)
  
An LED can be attached to pin 8 to monitor for any read errors.


SparkFun IR thermometer Evaluation Board - MLX90614
******************************************************************************/
#include <LiquidCrystal.h>
#include <Wire.h> // I2C library, required for MLX90614
#include <SparkFunMLX90614.h> 
const int motorpin1 = 10;
const int LED1=13;
const int triggerPin = 8; 
const int echoPin = 9;   
IRTherm therm; // Create an IRtherm object to interact with throughout

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() 
{
  Serial.begin(115200); // Initialize Serial to log output
  Wire.begin(); //Joing I2C bus
  
  if (therm.begin() == false){ // Initialize thermal IR sensor
    Serial.println("Qwiic IR thermometer did not acknowledge! Freezing!");
    while(1);
     lcd.print("Senosr Error :");
  }
  Serial.println("Qwiic IR thermometer did acknowledge.");
  lcd.print("Senosr OK :");
  
  therm.setUnit(TEMP_C); // Set the library's units to Farenheit
  // Alternatively, TEMP_F can be replaced with TEMP_C for Celsius or
  // TEMP_K for Kelvin.
  
  pinMode(LED1, OUTPUT);// LED pin as output
  pinMode(motorpin1,OUTPUT);
   digitalWrite(motorpin1, LOW); 
   pinMode(triggerPin, OUTPUT);
    pinMode(echoPin, INPUT);


}

void loop() 
{
  digitalWrite(LED1, HIGH);
      if (GetMeasuredDistance() <= 8 && therm.read())
  {
    // Use the object() and ambient() functions to grab the object and ambient
  // temperatures.
  // They'll be floats, calculated out to the unit you set with setUnit().
    Serial.print("Your Temp is: " + String(therm.object()+10, 2));
    Serial.println("C");
    delay(10);
        lcd.print("Your temp:");
        lcd.setCursor(0,1);
        lcd.print(String(therm.object()+10, 2) + "* F");

    Serial.print("Ambient: " + String(therm.ambient(), 2));
    Serial.println("F");
    Serial.println();
     lcd.print("Ambient temp:");
        lcd.setCursor(0,1);
        lcd.print(String(therm.ambient(), 2) + "*F");
if ( therm.object() > 101 ) {
    digitalWrite(motorpin1, HIGH);
    Serial.println ("Temp High ");
     lcd.print("Your temp:");
        lcd.setCursor(0,1);
  } else {
    digitalWrite(motorpin1, LOW);
    Serial.println ("Temp Normal ");
  }
    
        
  }
  else if (GetMeasuredDistance() >= 20)
    {
        lcd.print("Come closer :)");
    }

    else {
        lcd.print("The Ambiant temperature is :");
        lcd.setCursor(0,1);
        lcd.print(String(therm.ambient(), 2) + "*C");
    }

    delay(500);

 digitalWrite(LED1, LOW);
  delay(1000);}
  
int GetMeasuredDistance()
{

    long duration;

    digitalWrite(triggerPin, LOW);
    delayMicroseconds(5);

    digitalWrite(triggerPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(triggerPin, LOW);

    duration = pulseIn(echoPin, HIGH);

    return duration * 0.034 / 2; // This return a distance in cm
}