#include <math.h>
#include <LiquidCrystal_I2C.h>
#define TRIG 3
#define ECHO 2
#define SPEED_OF_SOUND 0.0343
unsigned long duration;
unsigned long distance;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup()
{
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
lcd.init();
lcd.backlight();
lcd.begin(16, 2);
lcd.clear();
}
void loop()
{
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
duration = pulseIn(ECHO, HIGH);
distance = round(duration * SPEED_OF_SOUND * 0.5);
lcd.print(distance);
lcd.print("cm");
lcd.setCursor(0,1);
lcd.print(distance < 20 ? "danger" : "safe");
lcd.setCursor(0,0);
}