1#include <LiquidCrystal.h>
2
3LiquidCrystal lcd(9,8,4,5,6,7);
4
5const
6 int trigPin = 3, echoPin = 2;
7long duration;
8int distanceCM, distanceINCH;
9
10void
11 setup() {
12 lcd.begin(16,2);
13 pinMode(trigPin, OUTPUT);
14 pinMode(echoPin,
15 INPUT);
16
17}
18
19void loop() {lcd.clear();delay(10);
20 digitalWrite(trigPin,
21 LOW); delayMicroseconds(2);
22 digitalWrite(trigPin, HIGH); delayMicroseconds(10);
23
24 digitalWrite(trigPin, LOW);
25 duration = pulseIn(echoPin, HIGH);
26 duration to calculate distance
27 distanceCM = duration*0.034/2;
28 distanceINCH
29 = duration*0.0133/2;
30
31 lcd.setCursor(0,0);
32 lcd.print("Distance: ");
33
34 lcd.print(distanceCM); lcd.print(" cm");delay(10);
35
36 lcd.setCursor(0,1);
37
38 lcd.print("Distance: ");
39 lcd.print(distanceINCH); lcd.print("inch");delay(100);
40
41
42 if(distanceINCH>99){
43 lcd.clear(); delay(10);
44
45 }
46
47}