counter not working

my code below has three nested if statements.
but the if statement is only met when both state1 and state2 are equal to 4.
even when condition for printing "a","b" or "c" is met it does not print.
it is only printing "close" and "d" when their conditions are met.
please help.

const int sensor1=A0;
int count=0;
int check=0;
int state1=0;
int state2=0;
int anaval=0;

void setup() {
pinMode(sensor1,INPUT);
Serial.begin (9600);
}

void loop () {
calibrate ();

if (state1==state2){

if (count==1){

if (state1==1){
Serial.println("A");
count=0;
}
else if (state1==2){
Serial.println("B");
count=0;
}
else if (state1==3){
Serial.println("C");
count=0;
}
else if (state1==4){
Serial.println("D");
count=0;
}
}

else if (count==0){

if (state1==4){
Serial.println("close");
count=1;
}
}
}

}

void calibrate () {

anaval=analogRead(sensor1);

while (millis() <=500){
check= analogRead(sensor1);
}

if (anaval>=0 && anaval<=225){
state1=4;
}
else if (anaval>=226 && anaval<=510){
state1=1;
}
else if (anaval>=511 && anaval<=755){
state1=2;
}
else if (anaval>=756 && anaval<=1023){
state1=3;
}

//check
if (check>=0 && check<=225){
state2=4;
}
else if (check>=226 && check<=510){
state2=1;
}
else if (check>=511 && check<=755){
state2=2;
}
else if (check>=756 && check<=1023){
state2=3;
}

}

while (millis() <=500){
    check= analogRead(sensor1);
  }

millis() <= 500 is only true ~500 times every 49 days (or something like that).

shri1510:
...
...

Please read How to use this forum - please read, specifically point #7 about posting code. Next edit your post and add the code tags around your code.

arduino_new:

while (millis() <=500){

check= analogRead(sensor1);
 }




millis() <= 500 is only true once every 49 days (or something like that).

After uploading a sketch, the unsigned 32-bit systemTimer starts updating by 1 ms. The current content of the systemTimer can be known by executing the millis() function. As time moves in the forward direction, the 500 ms content of the systemTimer will come gain after 232 ms which is ~= 49 days (4 294 967 296 ms ==> 4 294 967 s ==>71 582 m ==> 1 193 h ==> 49 d).