HELP line following robot project

Hi all,
i am new to arduino programming and i am working on a project to create a line following robot.
The purpose is to have a robot follow a line to a target area where it will drop a block into target.
the target is 2 squares, one is 18 inches by 18 inches and inside of it is a smaller 6 inch by 6 inch square.
The line getting to the target is 2 inches wide and the width of the line of the tarted is 1 inch.

I am using 3 light dependent resistors on the front in a row where the one in the middle is always on the line and the two outer ones are on white background.
I have been able to code the line following part and it seems to work pretty well.

The problem i am having is when it gets to the target area. I was hoping to get it to recognize that all three sensor cross dark line and count the number of times it has. I need it to cross 4 lines before it stops and drops the block.
I have tried a FOR loop and WHILE loop but no matter what i do it seems to get stuck in the loop. Or when i have it increment a count it always changes values because the crossing of the line isnt instant.

Here is the line following code.

int a = analogRead(A3); //sets a to reading from A3
  
 
        }
  
  }

please, does anyone have any ideas on the crossing of the lines in the target area :slight_smile:

Something along the lines of comparing the reading to the last one. So if you get a black reading twice in a row, you're still on the same line. That might happen say ten times, then if you get a white you know you left the black line. Then a few whites followed by a black, means your're back on the black. Only then do you update the counter....

thanks for the input.
this is what i tried last

 if(a < thresA && b < thresB && c < thresC)  //this is for whent he sensors all become dark when it crosses first line
  
     for the servo to reach the position 
         } 
    }
      
  }

but for some reason it skips everything and goes to the last if where it turns on the servo and just continues to loop there. not caring where it is on the line

I didn't look further than this:

if(n = 4)

Read the warning here

wow thanks for catching that.
that fixed the problem of it skipping all line following code.
But now i have

  if(a < thresA && b < thresB && c < thresC)  //this is for when the sensors all become dark when it crosses first line
           // waits 15ms for the servo to reach the position 
         } 
    }
      
  }

when ever it gets to a white area where all 3 sensors detect white it counts up n to 4 in a split second.
instead of just uping n by 1.

i can get it to work some what right if i take out the while loop put a delay in the second to last if statement like below where the delay(1000) is.
But i dont know if i should do this since i dont know how long it will take to cross a white area

 if(a < thresA && b < thresB && c < thresC)  //this is for when the sensors all become dark when it crosses first line
  
      
  }

anyone have any ideas?

can anyone help. i think i am really close.

here is what i have so far

 int a = 0;
 

  }

from this when it passes a dark line which is a < thresA the counter n increases by 1.
but when it hits the white area the counter withink a split second keeps counting until 4.

i cant figure out how to put the counter in without it increasing on its own.

it should go like below as the robot moves

n=0
black line
n=1
white area
black line
n=2
white area
black line
n=3
white area
black line
n=4
trigger servo

thats how i want it to go
but since it is trigger all count 2 through 4 in the first white area it is more like

black line
n=1
white area
n=2
n=3
n=4
trigger servo

please anyone have any ideas?