Code works with a 10 milli delay but without it, the code goes crazy

So i have posted about this in the sensors page but everyone is commenting on the code itself and not what might be causing this. I was just hoping if anyone on this page might also be able to shed more light on the issue.

So what i have is a sharp ir sensor hooked up to an Arduino Duemilanove. I'm using the sensor as an optical encoder to track velocity. the encoder is attached to a training bicycle wheel and records the time it takes to pass from one space to the next. The wheel is solid and does not have spokes so the wheel surface has sections of alternating solid black and white sections. When i read the value of the section the sensor is reading i take a time and do not take another time reading until the sensor is in the next colored section.

I've calculated that i need to be able to read 250 pulses/sec and from what i get from arduino's analog page is that the analog function should be able to read at 2500 pulses/sec. Therefore i should be able to do this without a problem. however, when i run the code without a slight delay at the end it starts getting all different readings and runs through the entire code even though neither the bicycle wheel or the sensor are moving and nothing should be happening. On the other hand, if i place a 10 millisecond delay at the end before it loops through again the code runs perfectly.

I was wondering if anyone had any insight to why this might be. Could it be because i'm running at 9600 baud and not 115200? Would the 10 millisecond delay even effect the readings? Would writing the data straight to an SD card prevent this rather than writing it to the serial monitor?

Any advice would be great. the code I am using is below:

//Justin Tsuyuki
//Optical Endocer Code II

//User Changeable Variables
int desiredRevs = 10;
int WheelResolution = 10;

//DO NOT CHANGE ANYTHING BEYOND THIS POINT
int SensorPin = A0;                      //analog pin sensor is attached to 
unsigned long time1 = 0;
unsigned long time2 = 0;
unsigned long time3 = 0;
int Flag = 0;                            //primary condition
int desiredPulses = (WheelResolution * desiredRevs)/2;
unsigned long val = 0;
int couple = 0;                         //secondary condition; 1 = beginning of first black, 2 = beginning of white
int status1 = 0;
unsigned long deltaT = 0;
int pulses = 0;



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

}

void loop()
{
  while (pulses <= desiredPulses)        //continue running program til i have reached the desired limit
  {
    val = analogRead(SensorPin);        //read analog pin value
    //Serial.println(val);
    //pulses = pulses + 1;
    if (val <= 200)                      //if value is less than 200 (white section) enter if statement
    {
      Flag = 1;                          //classify flag=1 for primary condition
      if (Flag == 1 && couple == 0)      //if both of these conditions are satisfied we are in the first white space 
                                              //and the program should begin the rest of the sequence
                                              //This part of the code is only required for one purpose: to 
                                              //make sure we begin in a white section and not in a black section
      { 
        couple = 1;                      //change secondary condition to recongize next black section and not repeat reading 
                                              //the white section it is in
        time1 = micros();                //take time when it first enters white section
        //Serial.println("white");
        //Serial.println("time1");
        //Serial.println(time1);
      }
      else if (Flag ==1 && couple == 2)  //when both of these conditions are met we know 1 of 2 things: first we know that we have gone
                                              // from the begining of the very first white section read to the beginning of the next white 
                                              //section on the encoder; or, second we know that we have proceeded from the beginning of 
                                              //any previoius white section read to the beginning of the next white section on the encoder
                                              //in both situations this is the point at where we will calculate the difference between 
                                              ///the time it took from one white section to the next
      {
        time3 = micros();                //Take final time reading
        deltaT = time3 - time1;          //calculate difference from previous time to this one
        couple = 1;                      //revert secondary condition to correspond with next black section
        time1 = time3;                   //reset time 1 to be equal to this latest time we have read
        pulses = pulses + 1;             //add 1 to value of pulses so we will read our desired pulses and not have an infinite loop
        //Serial.println("white");
        //Serial.println(pulses);
        Serial.println(deltaT);          //print the time difference to the serial monitor
        Serial.println(pulses);          //print the # of pulses gone through thus far
      } 
    }
    else                                 //if the sensor value is greater than 200 we know it is in a black section and this part 
                                              //of the code is relevant
    {
      Flag = 0;                          //set primary condition
      if (Flag == 0 && couple == 1)      //If the code begins in a black section this will not effect anything because both flag 
                                              //and couple will be 0, the code will skip this entire statement and proceed until 
                                              //it reaches the first white section if we are reading this section after a previous 
                                              //white section is read than this condition will be met and couple will change to 2 
                                              //and for the rest of the code the only statements that will matter will be 
                                              //this one and the else if statement inside the previous if statement
      {
        couple = 2;                      //initialize couple to 2
        //Serial.println("black");
        
      }
    }
    delay(10);                           //!!!!!!!!!!!!!!!! THIS DELAY IS WHERE I HAVE THE QUESTIONS, I AM DELAYING THE CODE FOR 10 
                                                //MILLISECONDS INBETWEEN READINGS BUT IF I TAKE OUT THIS DELAY THEN THE CODE GOES 
                                                //CRAZY AS DESCRIBED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  }
}

You can paste the url of your other thread in the text: Creating an optical encoder with an ir sensor - Sensors - Arduino Forum

You have to remove that delay.
You can write messages to the serial port, but if you send too much text, the internal buffer in the Serial library gets full and the Serial.println() function will wait until the serial data is transmitted and that will slow things down. So you have to be careful with that.

I can't see what the code is doing. Can you give the variables 'Flag' and 'couple' a better name ?
For a state machine, most of the time a single variable is used to identify the current state.

There are also libraries for pulses. Perhaps someone knows a library for this ?