Counter with a proximity sensor begins at inconsistent values

Hello everyone.
For a second year project mechanical engineering we’re making a device that plays noughts and crosses autonomously. We’re using 2 DC motors for x and y movement and a cylinder for z movement. The steel icons are picked up with a magnet on the end of the cylinder.
To let the arduino know where the cylinder is located, we’re using proximity sensor connected via relays. Our code works sometimes, but there is a slight problem. Since we’re using a counter to check which position the cylinder is located on the x or y position this counter needs to begin with value 1, because in Home Position both sensors are high. Every time a hole in the metal slide(see pic) comes along, the sensor becomes high and thus the counter goes +1.
Now comes the problem; sometimes the counter is 1 in Home Position and our whole program works flawlessly, and other times it begins with a 0 on Home Position and the motors keep spinning because it doesn’t reach his end value we specified.
Does anyone know how we can fix this?

if (x == 88) //For selecting X
{
teller1=0;
while(teller1 < 2){ // naar magazijn X vanaf home positie
digitalWrite(10,HIGH); //LINKSSSSS1
digitalWrite(11,HIGH);
digitalWrite(8,LOW);
digitalWrite(9,HIGH);
digitalWrite(12,LOW);
digitalWrite(13,LOW);

sensor1waarde = digitalRead(7);

if (sensor1waarde != eindstand1) {

if (sensor1waarde == HIGH) {

teller1++;
delay(10);
}

}
eindstand1 = sensor1waarde;
}

if (teller1 == 2){

digitalWrite(9,LOW);
teller1 = 0;
delay(50);

This is a piece of code as an example.

Does anyone know how we can fix this?

Step 1 - post your whole program and use [­code] ... [­/code] tags instead of [­quote] ... [­/quote] tags when you do. If the program is too large to post inline then attach the .ino file to your post.

How and where do you initialize eindstand1?

Please post full code - can't help otherwise