#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int potPin = A2;
const int LdrPin = A3;
const int ledPin = 5;
const int LampPin = A0;
int potValue = 0;
int ldrValue = 0;
int threshold = 0;
volatile bool updateSensor = false;
void setup() {
// put your setup code here, to run once:
lcd.init();
lcd.backlight();
pinMode(ledPin, OUTPUT);
pinMode(LampPin, OUTPUT);
lcd.setCursor(0, 0);
lcd.print("Sistem Kontrol ");
lcd.setCursor(0, 1);
lcd.print("Lampu Taman Auto");
delay(3000);
lcd.clear();
setupTimer1_100ms();
}
void loop() {
// put your main code here, to run repeatedly:
if (updateSensor) {
updateSensor = false;
potValue = analogRead(potPin);
int ldrRaw = analogRead(LdrPin);
ldrValue = 1023 - ldrRaw;
threshold = map(potValue, 0, 1023, 0, 1023);
bool lampOn = ldrValue < threshold;
digitalWrite(LampPin, lampOn ? HIGH : LOW);
digitalWrite(ledPin, lampOn ? LOW : HIGH);
lcd.setCursor(0, 0);
lcd.print("POT: ");
lcd.print(potValue);
lcd.print(" LAMPU");
lcd.setCursor(12, 1);
lcd.print(lampOn ? "ON " : "OFF ");
lcd.setCursor(0, 1);
lcd.print("LDR: ");
lcd.print(ldrValue);
int len = String(ldrValue).length();
for (int i = 0; i < (9 - len - 4); i++) lcd.print(" ");
}
}
void setupTimer1_100ms() {
noInterrupts();
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 1562;
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS12) | (1 << CS10);
TIMSK1 |= (1 << OCIE1A);
interrupts();
}
ISR(TIMER1_COMPA_vect) {
updateSensor = true;
}