#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27,16,2);
Servo myservo1;
int pos1 = 0;
Servo myservo2;
int pos2 = 0;
#define echopin1 A1
#define trigpin1 A0
#define echopin2 A3
#define trigpin2 A2
#define LED1 4
#define LED2 3
long durasi1;
long durasi2;
int jarak1, jarak_max1 = 250;
int jarak2, jarak_max2 = 250;
void setup() {
myservo1.attach(5);
myservo2.attach(6);
pinMode(trigpin1,OUTPUT);
pinMode(echopin1,INPUT);
pinMode(trigpin2,OUTPUT);
pinMode(echopin2,INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(9, OUTPUT);
Serial.begin(9600);
lcd.begin(0,0);
lcd.init();
lcd.backlight();
lcd.clear();
}
void loop() {
digitalWrite(trigpin1,LOW);
delay(100);
digitalWrite(trigpin1,HIGH);
delay(100);
digitalWrite(trigpin1, LOW);
durasi1=pulseIn(echopin1,HIGH);
digitalWrite(trigpin2, LOW);
delay(100);
digitalWrite(trigpin2,HIGH);
delay(100);
digitalWrite(trigpin2, LOW);
durasi2=pulseIn(echopin2,HIGH);
jarak1= durasi1*0.034/2;
jarak2= durasi2*0.034/2;
Serial.print("jarak1: ");
Serial.println(jarak1);
Serial.println("cm");
Serial.print("jarak2: ");
Serial.println(jarak2);
Serial.println("cm");
if(jarak1<30)
{myservo1.write(0);
delay(500);
digitalWrite(LED1,HIGH);
digitalWrite(9,HIGH);
lcd.setCursor(0,0);
lcd.print("GATE IN: OPEN ");
delay(200);}
else
{myservo1.write(180);
digitalWrite(LED1,LOW);
lcd.setCursor(0,0);
lcd.print("GATE IN: CLOSE ");
delay(200);;
}
if(jarak2<30)
{myservo2.write(180);
delay(500);
digitalWrite(LED2,HIGH);
lcd.setCursor(0,1);
lcd.print("GATE OUT: OPEN ");
delay(200);}
else
{myservo2.write(0);
digitalWrite(LED2,LOW);
lcd.setCursor(0,1);
lcd.print("GATE OUT: CLOSE ");
delay(200);}
}