Question/help with While loop

First, let me say this forum has been very helpful! I jumped into the world of Arduino a few weeks ago and with the help of this forum I have a workable program that is successfully running a DIY laser maze.

I have an “Easy” button which enables 6 lasers.
Then, when the “Start” button is pressed, the timer is enabled - as well as the LDR sensors.

When the kids finish the maze, I have a “Stop” button that disables all lasers and the timer.

When I first wrote the program, I incorrectly had the “Stop” portion of the code placed under the if statement checking if both the easy button AND the start button had been pressed. The problem was that if someone pressed the Easy button without pressing the Start button, the stop button wasn’t working. So, I just added a copy of the entire Stop Button code outside of the Easy/Start button if statement.

Based on a suggestion from here, I am using a While loop whenever a LDR Read value exceeds a target value. I have a 2 sec window that is used to add penalty time and sound a siren. But, there is 1 laser that is close to the stop button. When my son tried the maze, he triggered the laser near the stop button and then pressed the stop button. Nothing shutdown.

If I add a copy of the stop button code inside the while loop, will this solve this problem? I do not think it would happen very often, but if I can eliminate this glitch I would like to.

Thanks

if (ldr5Read > volt){
         unsigned long penStart = millis();
         while (millis() - penStart < 2000){
                digitalWrite(siren, HIGH);
                unsigned long currentTimerMillis = millis();
                if(currentTimerMillis - previousTimerMillis > penaltyInterval) {
                // save the last time you changed the timer 
                   previousTimerMillis = currentTimerMillis;   

                // if the timer is off turn it on and vice-versa:
                   if (timerState == LOW)
                       timerState = HIGH;
                       else
                          timerState = LOW;
   
             // set the timer with the timerState of the variable:
             digitalWrite(timer, timerState);
                } 
         }
         digitalWrite(siren, LOW);
      }

stopV1 = digitalRead(stopPB);
     delay(10);
     stopV2 = digitalRead(stopPB);
        if (stopV1 != stopState || stopV2 != stopState){
          stopMode = 1;
          digitalWrite(laser1, LOW);
          digitalWrite(laser2, LOW);
          digitalWrite(laser3, LOW);
          digitalWrite(laser4, LOW);
          digitalWrite(laser5, LOW);
          digitalWrite(laser6, LOW);
          digitalWrite(laser7, LOW);
          digitalWrite(laser8, LOW);
          digitalWrite(laser9, LOW);
          digitalWrite(laser10, LOW);
          digitalWrite(laser11, LOW);
          digitalWrite(laser12, LOW);
          digitalWrite(siren, LOW);
          digitalWrite(timer, LOW);
          easyMode = 0;
          startMode = 0;          
         }
if (stopV1 != stopState || stopV2 != stopState){

Did you mean if (stopV1 != stopState && stopV2 != stopState){ ?

Posting snippets is not usually a good idea.

Posting snippets is not usually a good idea.

Agreed.

I also don't like:

if (ldr5Read > volt){

Now, several lines later, you have a } floating around that doesn't line up with the {. So, the program structure, for me, is harder to see.

if(ldr5Read > volt)
{
   // Some code here
}

takes exactly one more line, but the { and the } line up. With proper indenting in between (which the Tools + Auto format menu item is pretty good at), the block logic is easy to see.

When I first wrote the program, I incorrectly had the "Stop" portion of the code placed under the if statement checking if both the easy button AND the start button had been pressed. The problem was that if someone pressed the Easy button without pressing the Start button, the stop button wasn't working. So, I just added a copy of the entire Stop Button code outside of the Easy/Start button if statement.

Instead of moving the code to the proper place? I don't understand doing this.

unsigned long currentTimerMillis = millis();

For local variables, short names, like "now" are good.

          digitalWrite(laser1, LOW);
          digitalWrite(laser2, LOW);
          digitalWrite(laser3, LOW);
          digitalWrite(laser4, LOW);
          digitalWrite(laser5, LOW);
          digitalWrite(laser6, LOW);
          digitalWrite(laser7, LOW);
          digitalWrite(laser8, LOW);
          digitalWrite(laser9, LOW);
          digitalWrite(laser10, LOW);
          digitalWrite(laser11, LOW);
          digitalWrite(laser12, LOW);

Arrays and for loops are good. Let the compiler for the grunt work.

If I add a copy of the stop button code inside the while loop, will this solve this problem?

Functions are a good thing. Have the function return a boolean value, and break out of the while loop if the value is true.