The for loop don't execute, when everything seems fine in state machine code

Hi,

It's my first bigger project with Arduino - it's a "reaction time test" with three LED diodes and a two buttons.

At the start - green diode is Lightining and program waits until you press the button. Than it waits some random time (2, 10 sec) and than red diode turns on and a timmer measures the time from the moment that diode turned on until you press red button.

I want to program something like program break or interupt while somebody pressed the button to early and print the "false start message" at LCD screen and do test again. The problem is that in code everything seems correct, but the "beetwen" diode just blink and even it doesnt wait this random time and starts the test:

switch (currentState) {

    case IdleState:

    
      digitalWrite(ledIdlePin, HIGH);
      digitalWrite(buttonStartLedPin, HIGH);

      if (digitalRead(buttonStartPin) == LOW)
      {
        lcd.clear();
        digitalWrite(ledIdlePin, LOW);
        digitalWrite(buttonStartLedPin, LOW);
        digitalWrite(ledBeginPin, LOW);

        lcd.print(welcome_message2);
        lcd.setCursor(0,1);
        lcd.print(welcome_message3);
        lcd.setCursor(0,2);
        lcd.print(welcome_message4);
        lcd.setCursor(0,3);
        lcd.print(welcome_message5);
        delay(4000);
         currentState = DelayState;
      }

 WaitTime = random(2000, 10000);  //Losowanie czasu do odczekanie między zieloną a żółta diodą

      break;

     


      /*DelayState - "uzbrojenie" - zapala się żółta dioda, osoba czeka na czerwony guzik (komunikat informujący o dalszym postępowaniu) wyświetlany na ekranie komputera */

      case DelayState:
      {

        Reakcje.reset(); //Wykorzystanie funkcji StopWatch, w celu uniknięcia kłopotliwego użycia millis() oraz micross() - źródło w pierwszej linijce kodu.
        //Reset czasu przed kolejnym testem, tak aby pokazywał tylko ostatnią próbę

       
       
              digitalWrite(ledBeginPin, HIGH);

 
          lcd.clear();

          lcd.print(get_ready);
          lcd.setCursor(0,1);
          lcd.print(get_ready1); 

       for (int TimeElapsed = 0; TimeElapsed <= WaitTime; TimeElapsed++)
            {

          
              if (digitalRead(buttonStopPin) == LOW)
              {
                
                lcd.clear();
                lcd.print("False start");
                currentState = IdleState;
                break;

              }
              }


         currentState = LightOnState;
        }

'
The test works perfectly without "DelayState".

Appreciate every help :slight_smile:

We can't see all of your code.

We can't see what causes DelayState to be the value of currentState.

We can't see any reason for the piss-poor indenting. Use Tools + Auto Format BEFORE posting ALL of your code.

We can't see ANY proof that the for loop is not executed.

What I can see is that your for loop is, without all the stupid blank lines:

       for (int TimeElapsed = 0; TimeElapsed <= WaitTime; TimeElapsed++)
       {
           if (digitalRead(buttonStopPin) == LOW)
           {
               lcd.clear();
               lcd.print("False start");
               currentState = IdleState;
               break;
           }
       }

If the switch is not pressed when the for loop starts, there is NOTHING to make the loop take any time, so it will complete in a few hundred nanoseconds. I SERIOUSLY doubt that you could even blink while the for loop executes.

Thank you for help and advices.

I'm doing Arduino second week and it is the first time I was posting here or any forum for coders, so sorry for making mess. I will try to keep it organised.

Sorry for trouble