// YWROBOT
// Compatible with the Arduino IDE 1.0
// Library version:1.1
#include <LiquidCrystal_I2C.h>
#define PIN_TRIG 3
#define PIN_ECHO 2
// set the LCD address to 0x27 for a 20 chars and 4 line display
LiquidCrystal_I2C lcd(0x27,20,4);
float cm;
float mm;
float inches;
void setup()
{
Serial.begin(115200);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.print(" --> Distance <-- ");
delay(3000);
lcd.clear();
}
void loop()
{
// Start a new measurement:
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
// Read the result:
int duration = pulseIn(PIN_ECHO, HIGH);
cm = (duration / 58.475);
mm = (duration / 58.475 * 10);
inches = (duration / 148.475);
/*
Serial.print("Inches ");
Serial.print(inches, 2);
Serial.print("\t");
Serial.print("cm ");
Serial.println(cm, 2);
*/
lcd.setCursor(0,0);
lcd.print("mm");
lcd.setCursor(8,0);
lcd.print("cm");
lcd.setCursor(15,0);
lcd.print("inch");
lcd.setCursor(0,1);
lcd.print(mm, 1);
lcd.setCursor(8,1);
lcd.print(cm, 1);
lcd.setCursor(15,1);
lcd.print(inches, 1);
delay(2000);
lcd.clear();
}