Help with for loop

I am trying to eliminate several extra hardware features as I figure out more programming. I had been using a 555 timer circuit to run my clock pin, but after reading the tutorial on blink without delay, I have successfully "copied" enough of the code in the example to get a get a 1-sec toggle code in place.

I am struggling a bit on implementing a for loop to handle a "penalty" code that would basically use the exact code with the only difference being the Interval is smaller, so the clock will toggle faster. My trouble is coming because I only want the "penalty" code to run for 1 sec (Interval value should produce 5 rapid toggles in the 1 sec.)

I have only been able to find for loop examples where an int variable is used to intialize and then the for loop is ran for so many "cycles". I need to have it run for a set amount of time.

I have attempted to accomplish this below. I have not included an initiation or increment (I thought the learning section mentioned they could be left out). Please let me know if/what I have missed.

The first portion of the code is the 1-sec toggle I have tested. It works great. Starts counting after both PB's are pushed.

 if (easyMode == 1 && startMode == 1){
     //start the 1 sec toggle after both easy and start PB's are pressed
     unsigned long currentTimerMillis = millis();
           if(currentTimerMillis - previousTimerMillis > timerInterval) {   // timerInterval is 500
           // 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);
           } 
  }
     ldr1Read = analogRead(ldr1);
      if (ldr1Read > 900){
         digitalWrite(siren, HIGH);
         delay(20);
         digitalWrite(siren, LOW);
         unsigned long penStart = millis();
         for (; millis() - penStart < 1000;){
                unsigned long currentTimerMillis = millis();
                if(currentTimerMillis - previousTimerMillis > penaltyInterval) {   //penaltyInterval is 100
                // 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);
                } 
         }
      }

the for loop can be used with parts missing for(;;) is valid.

What is happening ( or not )?, use serial print commands to print out info ( just to see what is/isn't running ) at each major step.

for (; millis() - penStart < 1000;){

There are 2 other types of loops in C, the while and do..while. The difference between the two 'while' loops is that the condition is evaluated at the top for the while loop and at the bottom for the do-while loop. Hence the do-while will execute at least once and may exit at the bottom.

What you have here is more naturally expressed as

while (millis() - penStart < 1000) {

marco_c:

for (; millis() - penStart < 1000;){

There are 2 other types of loops in C, the while and do..while. The difference between the two 'while' loops is that the condition is evaluated at the top for the while loop and at the bottom for the do-while loop. Hence the do-while will execute at least once and may exit at the bottom.

What you have here is more naturally expressed as

while (millis() - penStart < 1000) {

So, if I use a while loop, and I switch to the faster toggle rate when the while condition is true (1 second if that condition portion works as planned), after the while loop ends will the program switch back to the 1 sec toggle rate? If I have a siren outpin pin that is normally LOW and I digitally Write it HIGH inside the while loop will it go back to LOW when the while loop ends or do I need to digitallyWrite it LOW again?

A universal truth about computing machines is that NOTHING happens unless you explicitely make it happen, so you need to write it low if you want it low.