After first loop in void loop() function, arduino skips Serial.available

Hello everyone

Its my first post here, so I hope I dont make any mistake in asking my question. Thank you in advance.

Im programming a program to measure a reflex time of a person using a sensor and a LED that will randomly light up. It all works fine as I want, but after the first test I want the arduino to go back to the start, and wait again for user input saying the user is ready, and it doesnt, simply lightens up the LED at a random time.

Here ´s the code with the translations ( The println are in my language):

byte ledPin = 13;
byte sensorPin = 12;
unsigned long startTime;
unsigned long endTime;
float duration;




void setup() {
  // put your setup code here, to run once:

pinMode(ledPin, OUTPUT);
pinMode(sensorPin,INPUT);
Serial.begin(9600);
Serial.println("Bem vindo ao programa de teste das suas reaçoes! Prima qualquer botao para iniciar."); // ASKING USER TO TELL WHEN READY

while(Serial.available()==0){};

}

void loop() {
  
  Serial.println("Prima qualquer tecla quando estiver pronto a começar o teste."); // ASKING USER AGAIN WHEN READY

  while(Serial.available()==0){}
  
    delay(5000);

   randomSeed(analogRead(0));
   delay(random(1,6000));  

   digitalWrite(ledPin, HIGH);
   startTime=millis();
  while(digitalRead(sensorPin) == HIGH){}
  endTime = millis();
  duration = endTime - startTime;
  digitalWrite(ledPin, LOW);
  Serial.print("O tempo em segundos que demorou a tapar o sensor foi : ");
  Serial.println(duration/1000);
  }

The problem is only here :

void loop() {
  
  Serial.println("Prima qualquer tecla quando estiver pronto a começar o teste."); // ASKING THE USER TO INPUT ANYTHING TO INDICATE HE IS READY 
  
while(Serial.available()==0){}  // --->>>> SKIPPED 
  
    delay(5000);

   randomSeed(analogRead(0));
   delay(random(1,6000));  

   digitalWrite(ledPin, HIGH);
   startTime=millis();
  while(digitalRead(sensorPin) == HIGH){}
  endTime = millis();
  duration = endTime - startTime;
  digitalWrite(ledPin, LOW);
  Serial.print("O tempo em segundos que demorou a tapar o sensor foi : "); // INDICATES TIME THAT USER TOOK TO ACTIVATE THE SENSOR

  Serial.println(duration/1000);
}

Basically, everything works correctly, only in the first try. After the first "test" the first while(Serial.available()) doesnt seem to work.

Thank you for your help, I hope I made my question clear!

TheRealSlimShady:
Thank you for your help, I hope I made my question clear!

You need to post a complete program.

...R

Robin2:
You need to post a complete program.

...R

Edited. Im sorry, hope everything is fine now. ( I read the post saying how to ask for help, and one of the parameters were "Dont post hundreds of line of code" but I guess i missunderstood the indications).

Since you never read the available bytes from Serial, Serial.available() will continue to return > 0.
If you don't want to do anything with the Serial input you can just throw away the value of read():

  while (Serial.available() == 0) {}
  // Clear data from the Serial input buffer
  while (Serial.available() > 0) {
    Serial.read();
  }

You might expect Serial.flush() to provide this functionality but in fact it does something completely different.