#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int triggerPin = 2;
const int echoPin = 3;
const int LED1 = 4;
const int LED2 = 5;
const int LED3 = 6;
long durasi;
void setup() {
pinMode(triggerPin, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(echoPin, INPUT);
lcd.backlight();
lcd.init();
}
void loop() {
digitalWrite(triggerPin, LOW);
delay(400);
digitalWrite(triggerPin, HIGH);
delay(400);
digitalWrite(triggerPin, LOW);
durasi = pulseIn(echoPin, HIGH);
int cm = durasi*0.0342/2;
float m = durasi*0.0342/2/100;
if(cm < 100){
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
lcd.setCursor(0, 0);
lcd.print("JARAK: ");
lcd.print(cm);
lcd.print(" CM ");
lcd.setCursor(5, 6);
lcd.print("BERBAHAYA");
lcd.setCursor(0, 1);
lcd.print("KET: ");
}else
if(m > 1 && m < 2){
digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, LOW);
lcd.setCursor(0, 0);
lcd.print("JARAK: ");
lcd.print(m);
lcd.print(" M ");
lcd.setCursor(5, 6);
lcd.print("WASPADA ");
lcd.setCursor(0, 1);
lcd.print("KET: ");
}else
if(m > 2){
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
lcd.setCursor(0, 0);
lcd.print("JARAK: ");
lcd.print(m);
lcd.print(" M ");
lcd.setCursor(5, 6);
lcd.print("AMAN ");
lcd.setCursor(0, 1);
lcd.print("KET: ");
}
}