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!