Using millis() to create a single time delay

I understand how to get through a millis() rollover. However, perhaps this is not such a good idea:

 // If it is the first time through this function, set the startTime = current time. 
//  On subsequent passes, this step will be skipped.
    
    if (startTime == 0) {
    startTime = millis();

startTime is zero (maybe) the first time through. It might also be zero 49.7 days from now, or 99.4 days from now, or ...

Also: I said "(maybe)" because it depends upon how much initialization takes place before the program gets here.

vaj4088: millis() rolls over every 49.7 days or so. If your Arduino program is going to be running this long, AND if this code could be executed when millis() == 0, then startTime could get set to 0 once again. Maybe the boolean flag is needed after all?

One time (first time) setup should be placed in the setup() function. Unfortunately, you then may need to have some global variables, and this may make some people unhappy.

vaj4088 - No worries about the millis roll over. StartTime will be reset back to 0 approximately 7-10 seconds later in the sketch.

The Arduino is being used to control a K'nex model roller coaster with two cars on the same track. This time delay is used to let the riders exit the coaster car before the gates open allowing the new riders to enter the coaster car. I have a switch to detect both the closed gates and open gates. Plan to use the open gate switch to trigger a reset of StartTime for the next car that arrives at the station. I figure that will take 10 seconds max.

Thanks for your input and recommendation. It is appreciated. I would be very curious to know how to modify my sketch to eliminate the global variable gateOpenDelayComplete. I am new to Arduino and don't really understand why it is considered such poor programming practice to use global variables. I really want to do this the right way. Just don't understand what I am doing that is so bad.

Don

dz63: ...don't really understand why it is considered such poor programming practice to use global variables.

In general you want to limit the scope as much as possibly (e.g. local variables are limited in scope to the function in which they are declared). Limiting the scope simply makes it impossible for certain types of bugs to occur and provides information to the future you when you are doing maintenance.

[quote author=Coding Badly link=topic=232175.msg1676387#msg1676387 date=1397253390]

dz63: ...don't really understand why it is considered such poor programming practice to use global variables.

In general you want to limit the scope as much as possibly (e.g. local variables are limited in scope to the function in which they are declared). Limiting the scope simply makes it impossible for certain types of bugs to occur and provides information to the future you when you are doing maintenance.

[/quote]

I would agree completely with @Coding Badly if the program was for a PC.

But I am quite happy to use global variables on the Arduino which has only a trivial amount of RAM. I find global variables are an easier way for me to keep track of my RAM usage. They also avoid the need for complex return values from functions.

However if you are running out of RAM non-static local variables can be a useful way to save RAM because most of the time the compiler creates them in registers.

...R