#include <LiquidCrystal.h> // Include LiquidCrystal library for LCD display functionality
//LiquidCrystal lcd(RS, E, D4, D5, D6, D7);
LiquidCrystal lcd(2, 4, 9, 10, 11, 12); // Initialize the arduino pins connected to LCD
#define PIN_TRIG A3 // Define the pin connected to the trigger pine of the ultrasonic sensor
#define PIN_ECHO A4 // Define the pin connected to the echo pine of the ultrasonic sensor
void setup() {
Serial.begin(115200); // Begin serial comunication at a baud rate of 115200
pinMode(PIN_TRIG, OUTPUT); // Set the trigger pin as an output
pinMode(PIN_ECHO, INPUT); // Set the echo pin as an input
lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
lcd.print("Distance in:"); // print the charecters on the first row
}
void loop() {
// Start a new measurement:
digitalWrite(PIN_TRIG, HIGH); // set the trigger pin high to send a pulse
delayMicroseconds(10); // wait for 10ms
digitalWrite(PIN_TRIG, LOW); // set the trigger pin low to end the pulse
// Read the result:
int duration = pulseIn(PIN_ECHO, HIGH); // mesure the duration ot pulse on echo pin
double distance = (duration * 0.0343) / 2; // calculate the distance in cm based on the duration devided by to becuse the pulse send from sensore and again reflection form object and recive
// lcd.clear(); // clear the LCD
// these code print on lcd
lcd.setCursor(0, 1); // move the cursor to the secend row
lcd.print(distance); // print the distance
lcd.print(" CM "); // print "CM"
// these code print on serial monitor like computer
Serial.print(distance); // print the distance on computer
Serial.println(" CM"); // print "CM" on computer
delay(700); // wait for 700ms
}