/* Measuring the distance with an ultrasonic range finder
and showing the distance in cm on an LCD
https://github.com/ostad-ai/Arduino-Tutorial
*/
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 10, d5 = 9, d6 = 8, d7 = 7;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
const int echo_pin=5,trig_pin=6;
void setup() {
// put your setup code here, to run once:
lcd.begin(16,2);
lcd.print("Distance in cm:");
pinMode(echo_pin, INPUT);
pinMode(trig_pin,OUTPUT);
//clear the trigger
digitalWrite(trig_pin,LOW);
delayMicroseconds(2);
}
void loop() {
// put your main code here, to run repeatedly:
// trigger: send a HIGH signal for at least 10 microseconds
digitalWrite(trig_pin,HIGH);
delayMicroseconds(10);
digitalWrite(trig_pin, LOW);
//read the length of HIGH pulse in microseconds
long duration=pulseIn(echo_pin,HIGH);
lcd.clear();
lcd.print("Distance in cm:");
lcd.setCursor(0,1);
lcd.print(duration/58);
delay(100);
}