#include <LiquidCrystal.h>
LiquidCrystal lcd(9,8,4,5,6,7);//(rs,e,d4,d5,d6,d7)
const int trigPin = 3, echoPin = 2;
long duration;
int distanceCM, distanceINCH;
void setup() {
lcd.begin(16,2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {lcd.clear();delay(10);
digitalWrite(trigPin, LOW); delayMicroseconds(2);
digitalWrite(trigPin, HIGH); delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);//determine duration to calculate distance
distanceCM = duration*0.034/2;
distanceINCH = duration*0.0133/2;
lcd.setCursor(0,0);
lcd.print("Distance: ");
lcd.print(distanceCM); lcd.print(" cm");delay(10);
lcd.setCursor(0,1);
lcd.print("Distance: ");
lcd.print(distanceINCH); lcd.print("inch");delay(100);
if(distanceINCH>99){//for displaying purposes
lcd.clear(); delay(10);
}
}
Comments