#include <Servo.h>
Servo myservo;
// Pin untuk sensor LDR
const int ldrPin1 = A0;
const int ldrPin2 = A1;
// Pin untuk servo
const int servoPin = 9;
// Threshold perbedaan nilai antara kedua LDR
const int threshold = 10;
void setup() {
Serial.begin(9600);
myservo.attach(servoPin);
myservo.write(90); // Posisikan servo di tengah saat start
delay(1000);
}
void loop() {
int ldrValue1 = analogRead(ldrPin1);
int ldrValue2 = analogRead(ldrPin2);
// Menampilkan nilai LDR ke Serial Monitor
Serial.print("LDR 1: ");
Serial.print(ldrValue1);
Serial.print(" | LDR 2: ");
Serial.println(ldrValue2);
int difference = ldrValue1 - ldrValue2;
if (abs(difference) > threshold) {
int currentPosition = myservo.read();
if (difference > 0) {
// LDR 1 mendapatkan lebih banyak cahaya
currentPosition = currentPosition + 1; // Putar servo ke kiri
} else {
// LDR 2 mendapatkan lebih banyak cahaya
currentPosition = currentPosition - 1; // Putar servo ke kanan
}
// Batasi gerakan servo antara 0 hingga 180 derajat
currentPosition = constrain(currentPosition, 0, 180);
myservo.write(currentPosition);
delay(50); // Tambahkan sedikit delay untuk mengurangi jitter
}
delay(100); // Delay utama loop
}