Ultrasonic Parking Sensor with LCD

Hallo everybody, im sorry if i posted in the wrong place, i have noticed that some people want to build a parking sensor but not the correct code is supplied, over the weekend i built a parking sensor for my car that works with a HC-SR04 ultrasonic sensor and a LCD screen with backpack, I used an UNO R3 board for the design and then later moved it onto a trinket to minimise size.

It works very simply,
The distance in sound is converted to cm ( if you want inches then let me know if you cant figure it out yourself). When an object is out of range (more than 100cm away) the LCD screen displays "NO DANGER" if an object comes within 100cm of the car the LCD will then say "OBJECT WARNING" until the object reaches a distance of 6cm from the car then a buzzer that is connected to pin 12 will sound intermitantly and the LCD screen will also start to flash repeatedly until the object moves further away from the car, this way you have a visual warning if your music is to load and you cant hear the buzzer and a flickering screen surely will catch your attention. For the LCD backpack i used the hd44780 I2C libriary, if you are not using a back pack the unit will still work by changing to the normal LCD libriary and connecting all the pins up. You should be able to understand the connections from the code, if you cant figure it out just drop me a text and ill send you a drawing. Just make sure you have the correct libriaries installed.

#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h> 

hd44780_I2Cexp lcd; 
const int LCD_ROWS = 2;
const int LCD_COLS = 16;

const int buzzerPin = 12;
const int trigPin = 13;
const int echoPin = 11;
long duration;
int distanceCm;

void setup()
{
  lcd.begin(LCD_COLS, LCD_ROWS);
    lcd.backlight();
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
}

void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distanceCm = duration*0.034/2;

lcd.setCursor(0,1); 
lcd.backlight();
lcd.print("Distance: "); 
lcd.print(distanceCm); 
lcd.print(" cm");
delay(10);
lcd.clear();

if (distanceCm < 100){
  lcd.setCursor(0,0);
  lcd.print(" OBJECT WARNING");
}else{
  lcd.setCursor(0,0);
  lcd.print("   NO DANGER");
}
if (distanceCm <= 8){
  digitalWrite(buzzerPin, HIGH);
  lcd.backlight();
  delay(500);
  digitalWrite(buzzerPin, LOW);
  lcd.noBacklight();
  delay(500);
}
}

i have noticed that there is some undesirable flickering on the LCD screen here is the new code to repair it so that the LCD scree displays a more stable value.

#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h> 

hd44780_I2Cexp lcd; 
const int LCD_ROWS = 2;
const int LCD_COLS = 16;

const int buzzerPin = 12;
const int trigPin = 13;
const int echoPin = 11;
long duration;
int distanceCm;

void setup()
{
 lcd.begin(LCD_COLS, LCD_ROWS);
   lcd.backlight();
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(buzzerPin, OUTPUT);
}

void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distanceCm = duration*0.034/2;

lcd.setCursor(0,1); 
lcd.backlight();
lcd.print("Distance: "); 
lcd.print(distanceCm); 
lcd.print(" cm");
delay(10);


if (distanceCm < 100){
 lcd.setCursor(0,0);
 lcd.print(" OBJECT WARNING");
 delay(500);
 lcd.clear();
 
}else{
 
 lcd.setCursor(0,0);
 lcd.print("   NO DANGER");
 delay(1000);
 lcd.clear();
}
if (distanceCm <= 10){
 digitalWrite(buzzerPin, HIGH);
 lcd.backlight();
 delay(500);
 digitalWrite(buzzerPin, LOW);
 lcd.noBacklight();
 delay(500);
}
}