Two axis solar tracking system

Al-salam Alaikum (hello)
I worked recently on a project for my MSc degree, I need to follow the path of the sun via mathematical formulas(azimuth and Altitude), in other hand I used a combination of 4 LDR (Light Dependent Resistor) with sheets between them as a sensor to trim the error that caused by mathematical formulas as shown in the following link
http://cdn.instructables.com/FBN/N0RK/GPBD4JQV/FBNN0RKGPBD4JQV.LARGE.jpg
There is many project on internet which is used LDRs but with servo motor , my system is 2 DC motors on a mechanical structure with gear arrangement. now I need your help to direct the system to the sun by LDR only , I faced problems with my code
and here is my code

const int M2D=8;
const int M2U=9;
const int M1L=10;
const int M1R=11;
int A;
int B;
int C;
int D;
int a;
int b;
int c;
int d;
int ava1;
int ava2;
int ava3;
int ava4;
void setup()
{ pinMode(M1L,OUTPUT);
pinMode(M1R,OUTPUT);
pinMode(M2U,OUTPUT);
pinMode(M2D,OUTPUT);
Serial.begin(9600); }
void loop()
{ A=analogRead(A0);
B=analogRead(A1);
C=analogRead(A2);
D=analogRead(A3);
a=A*5/1024;
b=B*5/1024;
c=C*5/1024;
d=D*5/1024;
ava1=(a+b)/2;
ava2=(c+d)/2;
ava3=(a+c)/2;
ava4=(b+d)/2;
if (ava3 > ava4)
{digitalWrite(M1R,HIGH);}
else if (ava3 <= ava4)
{digitalWrite(M1R, LOW);}
if (ava4 > ava3)
{digitalWrite(M1L,HIGH);}
else if(ava4 <= ava3)   
{digitalWrite(M1L, LOW);}
if (ava1 > ava2)
{digitalWrite(M2U, HIGH);}
else if(ava1 <= ava2)
{digitalWrite(M2U, LOW);}
if (ava2 > ava1)
{ digitalWrite(M2D, HIGH);}
else if(ava2 <= ava1)
{digitalWrite(M2D, LOW);}
delay(100);
}

when upload the code to arduino the system being unstable and does not oriented to sun either.
thanks to any one who trying to help me :slight_smile: :smiley:

It would help a lot if you named your variables TopLeft, BottomRight, etc. rather than A, B, C, D.

Your conditionals don't seem to allow for the motors to ever stop. You should add hysteresis so that when the averages are 'close enough' to equal the motor stops. Otherwise the motors will sweep back and forth as fast as they can. Something like this:

     if (TopAverage > BottomAverage + Hysteresis)
         MoveUp();
    else
    if (BottomAverage > TopAverage + Hysteresis)
          MoveDown();

I worked recently on a project I need code for I used a combination of 4 LDR (Light Dependent Resistor) but with 2 DC motors on a mechanical structure with gear arrangement. now I need your help to direct the system to the sun by LDR only plz help me.