Call to Serial.print() not working

I have a stepper motor moving an arm anticlockwise until it interupts a light beam, a counter records the number of steps: It then moves clockwise the same number of steps and then waits for a button to be pressed. It works, repeatedly moving between the two positions, except that the Serial.println statement only returns one message "No of steps = 0" however many times I run the cycle. I cannot see the mistake in the program, and hope one of you can. Thank you in anticipation.

// define pins
#define stepPin 3
#define dirPin 2
#define testPin 8

long flipStep = 0;  //Stepper motor step counter

void setup() {
  Serial.begin(9600);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(testPin, INPUT_PULLUP); //Button connects to GND when pushed
  }


void loop() {
   digitalWrite(dirPin, LOW);  //Anticlockwise   
  while (analogRead(A1) > 250) {  //Light on sensor
    step(2000);  //Call to single step at speed dictated by pulse duration
    flipStep++;
  }
  Serial.print("No of steps = ");
  Serial.println(flipStep);
  digitalWrite(dirPin, HIGH);  //Clockwise
  while (flipStep > 0) {
    step(2000);
    flipStep--;
  }
  while (digitalRead(testPin) == HIGH) {  //When button is pressed, program moves on
  //Do nothing
  }

}

void step(int speed) {
  digitalWrite(stepPin,HIGH); 
  delayMicroseconds(speed);
  digitalWrite(stepPin,LOW); 
  delayMicroseconds(speed);
}

@paul9531031

Hi

Try testing the first while loop with

while (analogRead(A1) > 250) {  //Light on sensor
    step(2000);  //Call to single step at speed dictated by pulse duration
    Serial.println(flipStep++);
  }

Neither can I, other than if the while loop wasn't entered, but then the motor wouldn't move AFAICT. If it were my code I'd print the value of flipStep in a line added at the end of the first while loop, after flipStep++; For completeness I'd do it at the end of the second loop too.
[Edit, I wrote the above and posted it before I saw what @indev2 said]

@indev2
Thank you for your suggestion - it works, and at the end of each list is "No of steps = 138" for example. I cannot see why that should be - perhaps a timing issue?

Your topic doesn't indicate a problem with the IDE and hence had been moved to a more suitable location on the forum.