// Include the servo motor library
#include <Servo.h>
// Define the LDR sensor pins in the Analog pins
#define LDR1 A0
#define LDR2 A1
// Servo:
Servo turner; // Create an object for the servo motor
#define error 10 // Define the error value. This eliminates unnecessary movements
int Spoint = 90; // Starting point of the servo motor
void setup() {
// Include servo motor PWM pin
turner.attach(12);
// Set the starting point of the servo
turner.write(Spoint);
}
void loop() {
// Get the LDR sensor values
int ldr1 = analogRead(LDR1);
int ldr2 = analogRead(LDR2);
// Get the difference of these values
int errorCheck = abs(ldr1 - ldr2);
if (errorCheck > error) {
if (ldr1 > ldr2) {
Spoint = Spoint + 1; // LDR 1 is receiving more light
}
else if (ldr1 < ldr2) {
Spoint = Spoint - 1; // LDR 2 is receiving more light
}
}
// Write values on the servo motor
turner.write(Spoint);
delay(80);
}