#include <LiquidCrystal_I2C.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo servo;
#define LED 13
#define PIN_ECHO 2
#define PIN_TRIG 3
//volatile int state = LOW;
void setup() {
// put your setup code here, to run once:
lcd.init();
lcd.backlight();
pinMode(LED, OUTPUT);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
servo.attach(11);
}
void loop() {
// put your main code here, to run repeatedly:
float jarak, waktu;
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
waktu = pulseIn(PIN_ECHO, HIGH);
jarak = waktu / 58;
if (jarak > 200){
servo.write(90);
lcd.clear();
digitalWrite(LED, LOW);
delay (500);
lcd.setCursor(3, 0);
lcd.print("HATI-HATI!");
lcd.setCursor(2, 1);
lcd.print("REL DI DEPAN");
digitalWrite(LED, HIGH);
delay (1000);
}
else{
servo.write(0);
lcd.clear();
delay (500);
lcd.setCursor(1, 0);
lcd.print("AWAAAS KERETA!");
lcd.setCursor(4, 1);
lcd.print("STOOOP!!!");
digitalWrite(LED, HIGH);
delay (1000);
}
}