#include <LiquidCrystal_I2C.h>
#include <Servo.h>
int ldrPin = A0; // LDR pin
int servopin = 3;
int ledPin = 2; // Build in LED pin
Servo myservo;
LiquidCrystal_I2C lcd(0x27,16,2);
void setup() {
Serial.begin(9600); // Initialise the serial monitor
myservo.attach(servopin);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
lcd.init();
lcd.backlight();
}
void loop() {
int ldrVal = analogRead(ldrPin); // Read the analog value of the LDR
ldrVal = map(ldrVal, 0, 1023, 0, 180);
myservo.write(ldrVal);
if (ldrVal < 200) { // If the LDR value is lower than 200
digitalWrite(ledPin, HIGH); // Turn buildin LED on
} else {
digitalWrite(ledPin, LOW); // Turn buildin LED off
}
Serial.print("Nilai ADC: ");
Serial.println(ldrVal);
delay(1000); // Pause 1000ms
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sudut Derajat");
lcd.setCursor(0, 1);
lcd.print("Servo: ");
lcd.print(myservo.read());
delay(1000);
}