Problem with loop holding up the program execution

Hi.
I’m writing a program for a scoreboard and I’m adding in a flashing light and siren as part of it.

To run the “flash on” and “flash off” to indicate that the siren will sound at the expired time, rather than flashing the light with the delay function (and it would pause the whole program during execution), I’m using the millis() function instead.

However, it’s still holding up the whole program and I’ve never come across this before. Can someone please check the “part code” below and see if they can work out why?

byte strobePin = 16;
unsigned long strobeStartTime = 0;          // used to time the flashing of the strobe
int strobeOn = 200;                         // strobe on time
int strobeOff = 2000;                       // strobe off time

enum strobeState {
  flashing,
  not_flashing
};

int strobeState = not_flashing;
// alot of other variables here...


//---------------------------------
void setup(void) {
  pinMode(strobePin, OUTPUT);
  digitalWrite(strobePin, LOW);  // I used an analogue pin here - running out of pins!
  // alot of other setup here....
};

//---------------------------------
void loop(void) {
  strobeCheck();
  // 5 other functions called up here - that stop!???
} // end void loop

void strobeCheck (void) {
  if(strobeState == flashing && strobe == 1) {
    strobeStartTime = millis();
    while (millis() - strobeStartTime < strobeOn) {
      digitalWrite(strobePin, HIGH);
    } // end while
    strobe = 0; // turn off light
  } // end if
  if (strobeState == flashing & strobe == 0) {
    strobeStartTime = millis();
    while (millis() - strobeStartTime < strobeOff) {
      digitalWrite(strobePin, LOW);
    } // end while
    strobe = 1; // turn light back on
  } // end if
} // end strobeCheck function

you're using while loops. Those will hold up execution as long as that condition is true. I think you just want to do that once, though, so you should be using an if statement...

What changes strobeState to not_flashing so you can get out of strobecheck?

Not the cause of the problem, but below line also seems wrong

  if (strobeState == flashing & strobe == 0) {

Shouldn’t that be a double ‘&’ ?

DrAzzy:
you're using while loops. Those will hold up execution as long as that condition is true...

Thanks for that. I wasn't aware of the while loop holding up programs. I'm new to using them. I did change my code to an if statement instead (having setup strobeStartTime = millis(); at the start of the program) and it works perfectly (see amended part code below). PS. CrossRoads - sorry, I didn't include that in the thread here. It is a push button that flips strobeState to flash or not flash)

Amended coded working:

byte strobePin = 16;
unsigned long strobeStartTime = millis();      // used to time the flashing of the strobe
int strobeOn = 200;                               // strobe on time
int strobeOff = 2000;                             // strobe off time

enum strobeState {
  flashing,
  not_flashing
};

int strobeState = not_flashing;
// alot of other variables here...


//---------------------------------
void setup(void) {
  pinMode(strobePin, OUTPUT);
  digitalWrite(strobePin, LOW);  // I used an analogue pin here - running out of pins!
  // alot of other setup here....
};

//---------------------------------
void strobeCheck (void) {
  if(strobeState == flashing && strobe == 1 && millis() - strobeStartTime > strobeOff) {
    strobeStartTime = millis();
    digitalWrite(strobePin, HIGH);
    strobe = 0;
    return;
  } // end if
  if(strobeState == flashing && strobe == 0 && millis() - strobeStartTime > strobeOn) {
    strobeStartTime - millis();
    digitalWrite(strobePin, LOW);
    strobe = 1;
  } // end if
} // end strobeCheck function
};

? ? ? ?

Henry_Best:

};

? ? ? ?

Yea, it does look odd.
However with the enum function, you typically write it as follows (as I can see in the literature):

enum  counter {
  wait,
  commence,
  complete
};

The second last string doesn't have a comma and the last has a semi colon. There must be logic in there somewhere - but I don't know where it is!

The demo Several Things at a Time illustrates the use of millis() to manage timing.

Your Title is interesting. Most people come here with code that prevents loop() from repeating fast enough. I have never heard of loop() causing a hold up.

...R

Unless of course, you while {} away the time. :grinning:

Robin2:
Your Title is interesting. Most people come here with code that prevents loop() from repeating fast enough. I have never heard of loop() causing a hold up

Yes, the title was a bit incorrect. I sort of assumed a while statement was a type of a loop. Just showing my lack of code knowledge.

cjcj:
Yes, the title was a bit incorrect. I sort of assumed a while statement was a type of a loop. Just showing my lack of code knowledge.

It is, but since you didn't define the type of loop. I believe that Robin2 just misunderstood your meaning