#include <Servo.h> // library servo
#include <LiquidCrystal_I2C.h> // library LCD
// bagian dari library servo
Servo servo;
// bagian dari library LCD
LiquidCrystal_I2C lcd(0x27,16,2);
const int trigPin1 = 12;
const int echoPin1 = 11;
int angle = 10;
long duration;
int distance;
void setup() {
// put your setup code here, to run once:
servo.attach(8);
servo.write(angle);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(4, 0);
lcd.print("Welcome");
lcd.setCursor(5, 1);
lcd.print("Akpil!");
delay(1000);
lcd.clear();
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration = pulseIn(echoPin1, HIGH);
distance = (duration / 2) / 29.1;
if(distance<10)
{
servo.write(90);
}
else
{
servo.write(0);
}
lcd.setCursor(4, 0);
lcd.print("Jarak");
lcd.setCursor(5, 1);
lcd.print(distance);
delay(3000);
lcd.clear();
}