I am trying to make a circuit that will always turn to pint towards the brightest light to maximise solar power with an arduino uno, a servo and a turntable. All the circuitry is good and my code is telling the motor to turn towards the light, but it won't stop when it gets there. it will just turn around and around and never stop. I'm new at this so I'm not sure what to do. Help please.
/*
* code to make a solar panel point towards the sun(or brightest light) with a servo motor
*/
#include <Servo.h> // include servo library
Servo myservo; // declare motor
int pos = 0; // servo motor position
int inputLeft = A0; // light sensor on the left (attached to analog pin 0)
int inputRight = A1; // light sensor on the right (attached to analog pin 1)
int left = 0; // value of left sensor
int right = 0; // value of right sensor
void setup() {
pinMode(inputLeft, INPUT); // tell the circuit board that the sensors are inputs
pinMode(inputRight, INPUT); // tell the circuit board that the sensors are inputs
myservo.attach(9); // tell the circuit board that the servo is on pin 9
}
void loop()
{
left = analogRead(inputLeft);
right = analogRead(inputRight);
if (left > (right + 20)) { // the +20 is a buffer to stop it for just jiggling around
pos--;
myservo.write(pos); // tell servo to move towards the right
delay(10);
}
else if (right > (left + 20)) {
pos++;
myservo.write(pos); // tell servo to move towards the left
delay(10);
}
else {
// do nothing
}
}