# Trouble with if() loop

I have some trouble with the following if() loop (its only part of the whole thing):

``````int ldr1 = A0;  // right ldr
int ldr2 = A1;  // centre ldr
int ldr3 = A2;  // left ldr
float stepsize = 1.8;
float angLR = 0.0;

}
void loop()
{

if((right > centre) && (angLR >= -90.00 && angLR <= 90.00))
{
stepperh->onestep(BACKWARD,DOUBLE);
angLR += stepsize;
Serial.print("Angle LR °");
Serial.println(angLR);
}
if((left > centre) && (angLR >= -90.00 && angLR <= 90.00))
{
stepperh->onestep(FORWARD,DOUBLE);
angLR -= stepsize;
Serial.print("Angle LR °");
Serial.println(angLR);
}
}
``````

Were right, left and centre are analog readings from LDR’s. Everything works fine when “angLR” is between -90 and 90. But once angLR is -90 it wont move towards positive direction (even though the right LDR has more light). It should go -90,-89.2,-87.4,… instead it does nothing. Same thing in the other direction when angLR = +90.

Any help is appreciated.

Note: all LDR’s are working correctly and are correctly assigned.

Since you didn’t post the entire sketch I can’t tell, but what have you done to ensure that the value of angLR doesn’t ever get less than -90 or greater than 90. If it does exceed those values, then your if’s will never be true.

if you don’t already do something like this consider this modification to your code

``````int ldr1 = A0;  // right ldr
int ldr2 = A1;  // centre ldr
int ldr3 = A2;  // left ldr
float stepsize = 1.8;
float angLR = 0.0;

}
void loop()
{

if((right > centre) && (angLR >= -90.00 && angLR <= 90.00))
{
stepperh->onestep(BACKWARD,DOUBLE);
angLR += stepsize;
Serial.print("Angle LR °");
Serial.println(angLR);
}
if((left > centre) && (angLR >= -90.00 && angLR <= 90.00))
{
stepperh->onestep(FORWARD,DOUBLE);
angLR -= stepsize;
Serial.print("Angle LR °");
Serial.println(angLR);
}
//  The following two lines of code ensure that angLR is never outside an
// acceptable range.
if (angLR < -90) angLR = -90;
if (angLR > 90) angLR = 90;
}
``````

You code only does something , when angLR is between -90 and +90. When it is less than -90, it will do nothing, there is no code for that.

If you want it to do something other than nothing, you will need another if ( ) for that, or change your logic.

If you are expecting if to loop, you have more troubles ahead.