#include <Servo.h>
#include <LiquidCrystal.h>
const int d4 = 8;
const int d5 = 9;
const int d6 = 10;
const int d7 = 11;
const int en = 12;
const int rs = 13;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int merah = 2;
const int kuning = 4;
const int hijau = 5;
const int trig = 6;
const int echo = 7;
Servo myServo;
long durasi;
int Jarak;
void setup() {
lcd.begin(16, 2);
lcd.print("#giniamathidup");
myServo.attach(3);
pinMode(merah, OUTPUT);
pinMode(kuning, OUTPUT);
pinMode(hijau, OUTPUT);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
durasi = pulseIn(echo, HIGH);
Jarak = durasi * 0.034 / 2;
Serial.print("Jarak : ");
Serial.println(Jarak);
if (Jarak <= 100) {
myServo.write(0);
digitalWrite(merah, HIGH);
digitalWrite(kuning, LOW);
digitalWrite(hijau, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Tutup");
delay(100);
}
else if (Jarak >= 150 && Jarak <= 250) {
myServo.write(90);
digitalWrite(merah, LOW);
digitalWrite(kuning, HIGH);
digitalWrite(hijau, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Setengah");
delay(100);
}
else if (Jarak >= 251) {
myServo.write(180);
digitalWrite(merah, LOW);
digitalWrite(kuning, LOW);
digitalWrite(hijau, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Buka");
delay(100);
}
}