//include the servo motor library
#include <servo.h>
//define the LDR sensor pins
#define LDR1 A0
#define LDR2 A1//define the error value,you can change it as you like
#define error 10
//starting point of the servomotor
int spoint= 90;
//create object for the servo mototr
Servo servo;
void setup() {
//include the servomotor PWM pin
servo.attach(11);
//set the starting point of the servo
servo.write(spoint);
delay(1000);
}
void loop() {
//get the LDR sensor value
int ldr1=analogRead(LDR1);
//get the LDR sensor value
int ldr2=analogRead(LDR2);
//get the differnece for these value
int value1 =abs(ldr1-ldr2);
int value2 =abs(ldr2-ldr1);
//check the value using if condition
if (value <= error) || (value <= error)){
}else{
if (ldr1 > ldr2) {
spoint= --spoint;
}
if (ldr1 < ldr2) {
spoint=++spoint;
}
}
//write values on the servo motor
servo.write(Spoint);
delay(80);
})
}
}
}