# Help fix my code!?

i have two cds Photo resistors on a breadboard, the code should read forward as i place the over the surface, but it just reads back right, left, right, left etc… heres my code. whats the problem?

``````int pRL = 2;
int pRR = 0;
int RL;
int RR;

void setup(void) {
Serial.begin(9600);
}

void loop(void) {
RL = analogRead(pRL);
RR = analogRead(pRR);

if (RL < (RR-20) || RL > (RR+20) && RR < (RL-20) || RR){
Serial.println("Forword");
}

if(RL > (RR-20) || RL < (RR+20)){
Serial.println("LEFT!");
}

if(RR > (RL-20) || RR < (RL+20)){
Serial.println("RIGHT!");
}

delay(100);

}
``````
``````  if (RL < (RR-20) || RL > (RR+20) && RR < (RL-20) || RR){
``````

Looks you stopped typing a little too soon. Or RR what?

Some parentheses might be in order, too, unless you know the precedence order of && and ||.

Not finishing the code was just a pasting problem. its there on my code. what do you mean parentheses on the || &&?

You have 4 comparisons being performed (RL < RR-20, RL > RR+20, etc.). You have some && and some || between them.

Suppose we replace the comparisons with single variables that can hod true or false.

Your if statement looks like this:

``````if(A || B && C || D)
{
}
``````

Does that mean (A or B) and (C or D)? Or, does it mean A or (B and C) or D?

Unless you know which it means, you should add extra parentheses to remove the ambiguity.

Even better, though would be to separate it into multiple if statements:

``````if(A or B)
{
if(C or D)
{
}
}
``````

jcbconway

while the way you have it might be correct, parentheses make it much easier to read and be SURE you have the right order. The question is, which is evaluated first the OR "||" or the AND "&&". it can get confusing. parentheses make it very clear and not ambiguous.