#define LDR_PIN 4
#define ECHO_PIN 14
#define TRIG_PIN 12
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
void setup() {
// put your setup code here, to run once:
pinMode(LDR_PIN, INPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Serial.begin(115200);
lcd.init();
lcd.clear();
lcd.backlight();
}
void loop() {
lcd.clear();
// put your main code here, to run repeatedly:
// this speeds up the simulation
lcd.setCursor(0,0);
if (digitalRead(LDR_PIN) == LOW) {
lcd.print("Cahaya : Terang");
} else {
lcd.print("Cahaya : Gelap");
}
// Clear the condition
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(10);
// Generate ultrasonic signal
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read ultrasonic signal
int duration = pulseIn(ECHO_PIN, HIGH);
// Calculating the distance
float distance = duration * 0.034 / 2;
lcd.setCursor(0,1);
lcd.print(distance);
delay(1700);
}