#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// For these LCD controls to work you MUST replace the standard LCD library from...
// https://github.com/marcoschwartz/LiquidCrystal_I2C
// Direct download https://github.com/marcoschwartz/LiquidCrystal_I2C/archive/master.zip
// Your project will not compile until this is done.
LiquidCrystal_I2C lcd_I2C_27(0x27,16,2); // set the LCD address for a 16 chars and 2 line display
int UltrasonicSensorCM(int trigPin, int echoPin) //Ultrasonic Sensor Code Auto Generated Return CM max distance 200
{
  long duration;
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(20);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  duration = duration / 59;
  if ((duration < 2) || (duration > 200)) return false;
  return duration;
}


void setup(){  // put your setup code here, to run once:
  lcd_I2C_27.init (); // initialize the lcd
  lcd_I2C_27.backlight();
  digitalWrite( 6 , LOW ); //set ultrasonic sensor trigPin 

  pinMode( 13 , OUTPUT);   // sets the digital pin as output
}

void loop(){  // put your main code here, to run repeatedly:
  lcd_I2C_27.clear();
  lcd_I2C_27.setCursor(0 , 0) ; // set the cursor, counting begins with 0 
  lcd_I2C_27.print( "Distance" ); // Print a message to the LCD.
  lcd_I2C_27.setCursor(0 , 1) ; // set the cursor, counting begins with 0 
  lcd_I2C_27.print(  UltrasonicSensorCM( 6 , 7 ) ); // Print a message to the LCD.
  if (UltrasonicSensorCM( 6 , 7 ) < 10 and (UltrasonicSensorCM( 6 , 7 ) != 0) )
    digitalWrite( 13 , HIGH ) ; // 
  
  else 
    digitalWrite( 13 , LOW ) ; 
  delay( 50 ); // waits a few milliseconds //

}
