#include <Servo.h>
Servo myServo; // Create a servo object to control the servo motor
int servoPin = 9; // Servo motor connected to PWM pin 9
int sensorA0 = A0; // LDR sensor 1 connected to analog pin A0
int sensorA1 = A1; // LDR sensor 2 connected to analog pin A1
void setup() {
myServo.attach(servoPin); // Attach the servo motor to the PWM pin
myServo.write(90); // Set initial servo position to 90 degrees (facing forward)
delay(1000); // Wait for 1 second
}
void loop() {
int valueA0 = analogRead(sensorA0); // Read the value from LDR 1
int valueA1 = analogRead(sensorA1); // Read the value from LDR 2
if (valueA0 > valueA1) {
// Decrease the servo angle if LDR 1 has more light
while (valueA0 > valueA1) {
myServo.write(myServo.read() - 1); // Decrease the servo angle by 1 degree
delay(15); // Small delay to allow the servo to move
valueA0 = analogRead(sensorA0); // Update the LDR values
valueA1 = analogRead(sensorA1);
}
} else if (valueA1 > valueA0) {
// Increase the servo angle if LDR 2 has more light
while (valueA1 > valueA0) {
myServo.write(myServo.read() + 1); // Increase the servo angle by 1 degree
delay(15); // Small delay to allow the servo to move
valueA0 = analogRead(sensorA0); // Update the LDR values
valueA1 = analogRead(sensorA1);
}
}
// If neither LDR is greater than the other, the loop will restart
delay(100); // Small delay before the next loop iteration
}