I need help in a simple program

I’m trying to do a reaction time counter. When the leds turn off, you have to press a button and your time is displayed on the monitor. However, when I press the button, nothing happens. Can you see if there’s anything wrong with the script?

byte button = 2;
unsigned long startTime;
unsigned long endTime;
unsigned long duration;
byte timerRunning;
int long randomDelay = 0;
String received = "";

void setup() {
 Serial.begin(9600);
 pinMode(4, OUTPUT);
 pinMode(6, OUTPUT);
 pinMode(8, OUTPUT);
 pinMode(10, OUTPUT);
 Serial.println("Type ready to start");

}

void loop() {
 if(Serial.available() > 0) {
received = Serial.readStringUntil('\n');
if (received == "ready") {
  digitalWrite(6, HIGH);
  digitalWrite(8, HIGH);
  delay(500);
  digitalWrite(4, HIGH);
  digitalWrite(10, HIGH);
  digitalWrite(6, LOW);
  digitalWrite(8, LOW);
  delay(500);
  digitalWrite(4, LOW);
  digitalWrite(10, LOW);
  digitalWrite(6, HIGH);
  digitalWrite(8, HIGH);
  delay(500);
  digitalWrite(6, LOW);
  digitalWrite(8, LOW);
  delay(2000);
  
  digitalWrite(4, HIGH);
  delay(1000);
  digitalWrite(6, HIGH);
  delay(1000);
  digitalWrite(8, HIGH);
  delay(1000);
  digitalWrite(10, HIGH);
  delay(1000);
  
  randomDelay = random(4000);
  delay(randomDelay);
  digitalWrite(4, LOW);
  digitalWrite(6, LOW);
  digitalWrite(8, LOW);
  digitalWrite(10, LOW);
  startTime = millis();
  timerRunning = 1;
  
  if(timerRunning == 1 && digitalRead(button) == LOW){
  endTime = millis();
  duration = endTime - startTime;
  Serial.println("Your time was: ");
    Serial.println(duration + String(" seconds"));
    delay(2000);
  }
 }
}
}

Reaction_game.ino (1.41 KB)

Never ever use delay() in your sketches, well only if you are fully aware of the evil it can cause.


Read these:

How and Why to avoid delay():
http://playground.arduino.cc/Code/AvoidDelay

Demonstration code for several things at the same time.
http://forum.arduino.cc/index.php?topic=223286.0

Multitasking:
Part 1:

Part 2:

Part 3:

Where’s your pinMode for button?
Do you have a pull-up?

Looks like all your code is inside...

if (received == "ready") {

Are you continuously sending “ready” over the serial port?
If not everything inside that if will only happen once.

There are no scripts on the Arduino, only sketches, programs, or source files. Scripts are only run by a run time interpreter.

@dsousa5

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.
It will help you get the best out of the forum in the future.
Other general help and troubleshooting advice can be found here.

      if (timerRunning == 1 && digitalRead(button) == LOW)
      {

If the button pin is not LOW the moment you read it, your sketch is going to do nothing but go back to the top of loop() and wait for serial input. You probably should check the button state more than once. Perhaps every time through loop() as long as the timer is running.

You should probably check to make sure the button is not pressed before you start the timer. That would be cheating.