#include <Servo.h>
int ldr = 4;
int led = 2;
int nilai;
Servo myservo;
void setup() {
Serial.begin(9600);
Serial.println("Kontrol Servo dengan sensor LDR");
myservo.attach(15);
pinMode(led, OUTPUT);
}
void loop() {
nilai=analogRead(ldr);
Serial.print(" nilai LDR: ");
Serial.println(nilai);
if (nilai < 500){
digitalWrite(led, HIGH);
myservo.write(45); // ke kiri
delay(1000);
myservo.write(0);
delay(1000);
myservo.write(90);
delay(1000);
Serial.print("Servo bergerak ke kiri");
}else{
digitalWrite(led, LOW);
myservo.write(135); // ke kanan
delay(1000);
myservo.write(180);
delay(1000);
myservo.write(90);
delay(1000);
Serial.print("Servo bergerak ke kanan");
}
delay(1000);
}