Library and Variables
#include <Servo.h>
This includes the Servo library, which provides the functionality to control Servo motors.
Servo servohori;
int servoh=0;
int servohLimitHigh=160;
int servohLimitLow=20;
Here, we're defining a Servo object (servohori
) and three integer variables. servoh
will store the current horizontal position of the servo. servohLimitHigh
and servohLimitLow
define the upper and lower limits of the servo's movement.
The same definitions are made for the vertical servo:
Servo servoverti;
int servov=0;
int servovLimitHigh=160;
int servovLimitLow=20;
Next, we define the pins to which our Light Dependent Resistors (LDRs) are connected:
int ldrtopl=2;
int ldrtopr=1;
int ldrbotl=3;
int ldrbotr=0;
Setup
void setup()
{
servohori.attach(10);
servohori.write(0);
servoverti.attach(9);
servoverti.write(0);
delay(500);
}
In the setup()
function, we attach our Servo objects to the appropriate pins on our Arduino (servohori
to pin 10 and servoverti
to pin 9). We then set the initial position of each servo to 0 degrees. A short delay of 500 milliseconds is added to allow the servos to reach their initial positions.
Loop
void loop()
{
servoh=servohori.read();
servov=servoverti.read();
int topl=analogRead(ldrtopl);
int topr=analogRead(ldrtopr);
int botl=analogRead(ldrbotl);
int botr=analogRead(ldrbotr);
In the loop()
function, the first two lines read the current position of the servos. Next, the light intensity at each LDR is read using analogRead()
.
int avgtop=(topl+topr)/2;
int avgbot=(botl+botr)/2;
int avgleft=(topl+botl)/2;
int avgright=(topr+botr)/2;
These lines calculate the average light intensity for the top, bottom, left, and right pairs of LDRs.
The next block of code is responsible for controlling the vertical servo (servoverti
). If the light intensity is higher at the top, it tries to increment the servo position. If it's higher at the bottom, it tries to decrement the position. If the light intensity is equal at the top and bottom, the servo position remains the same.
if(avgtop < avgbot)
{
if(servov < servovLimitHigh)
{
servoverti.write(servov + 1);
servov++;
}
delay(10);
}
else if(avgbot < avgtop)
{
if(servov > servovLimitLow)
{
servoverti.write(servov - 1);
servov--;
}
delay(10);
}
else
{
servoverti.write(servov);
}
Similarly, the next block of code controls the horizontal servo (servohori
). If
the light intensity is higher on the left, it tries to increment the servo position. If it's higher on the right, it tries to decrement the position. If the light intensity is equal on the left and right, the servo position remains the same.
if(avgleft > avgright)
{
if(servoh < servohLimitHigh)
{
servohori.write(servoh + 1);
servoh++;
}
delay(10);
}
else if(avgright > avgleft)
{
if(servoh > servohLimitLow)
{
servohori.write(servoh - 1);
servoh--;
}
delay(10);
}
else
{
servohori.write(servoh);
}
delay(50);
}
The delay(50)
at the end of the loop provides a small pause before the loop starts again, allowing the servos time to move to their new positions before they're potentially updated again.
Remember, in the conditional blocks that control servo movement, we first check if the new position would be within limits. If it is, then we change the position. This protects your servos from attempting to move beyond their set limits.