Untitled

mail@pastecode.io avatar
unknown
plain_text
10 days ago
697 B
2
Indexable
Never
#include <LiquidCrystal_I2C.h>
#include <Wire.h> 
#include <Arduino.h>

#define trig 9
#define echo 10

LiquidCrystal_I2C lcd(0X27,16,2);
void setup()
{
  Serial.begin(9600);
  pinMode(trig,OUTPUT);
  pinMode(echo,INPUT);
  
  lcd.init();                    
  lcd.backlight(); 
}

void loop()
{
  unsinged long duration;
  int distance;
  
  digitalWrite(trig,LOW);
  delayMicroseconds(2);
  digitalWrite(trig,HIGH);
  delayMicroseconds(10);
  digitalWrite(trig,LOW);
  
  duration = pulseIn(echo,HIGH);
  distance = int(duration/2/29.412);
  
  lcd.setCursor(0,0);
  lcd.print("distance = ");
  lcd.setCursor(0,1);
  lcd.print(distance); 
  delay (100);
 
}
Leave a Comment