Execution happening in wrong order?

Hey there, newbie here.

I'm trying to understand why this works.

Basically, there are 3 lights that are flashing. We'll call them Dispatch, Dispatch2, and Estop. Dispatch and Dispatch2, are connected, and flash at the same time, Estop flashes at a different speed.

I have it set so that Estop flashing speed is 100 milliseconds and the Dispatch and Dispatch2 speed is 1000 milliseconds.

So when the code runs, it runs in the order of Estop blinks first, then Dispatch and Dispatch2. Because Estop is only 100 milliseconds long, the code to make it blink needs to be executed a lot more often than the Dispatch and Dispatch2 code. But since the code is supposed to go in a logical order of Estop code runs first, then the Dispatch and Dispatch2 code runs. How does the Estop code run at least 10 times over in the time the Dispatch and Dispatch2 code runs once? It was my understanding that no other executing occurs while one if statement is being executed. That being said, while the if statement of the Dispatch and Dispatch2 code is being executed, the Estop code should not be able to be executed, basically making the Estop flash come to a halt until the Dispatch and Dispatch2 if statement is finished, and then the code loops around and the Estop code executes again.

My question, why does this happen? Does the code jump around to where if statements are being met at any time, no matter where the program currently is? Or can multiple if statements be executed at once?

I apologize for the lengthy explanation, I just wanted to make sure I made myself clear. Thanks to anyone that can answer my (hopefully) simple question.

Here is the code (attached for reference as well as here)

const int EstopLT = 3;
const int DispatchLT = 4;
const int EstopBT = 6;
const int DispatchBT = 7;
const int Dispatch2LT = 10;
const int Dispatch2BT = 11;

//Set initial variable states.
int DispatchON = HIGH;
int EstopON = LOW;
int EstopHX = HIGH;
int RCSEnable = HIGH;
boolean floorless;

//Blink timer variable setup.
long previousEstopMillis = 0;
long DispatchBLK = 1000;
long EstopBLKDown = 250;
long EstopBLKUp = 1000;
long previousDispatchMillis = 0;
long previousMillis = 0;
long EstopBLK = 250;
long EstopBLKFloorless = 100;
long previousEstopAlwaysMillis = 0;

void setup(){

//Set Pin Modes for Buttons and Lights
//Lights: OUTPUT
 pinMode(EstopLT, OUTPUT);
 pinMode(DispatchLT, OUTPUT);
 pinMode(Dispatch2LT, OUTPUT);
//Buttons: INPUT_PULLUP
 pinMode(EstopBT, INPUT_PULLUP);
 pinMode(DispatchBT, INPUT_PULLUP);
 pinMode(Dispatch2BT, INPUT_PULLUP);
 

//Initialize control over the keyboard.
 Keyboard.begin();
}

void loop() {
int EstopPOS = digitalRead(EstopBT);
int DispatchPOS = digitalRead(DispatchBT);
int Dispatch2POS = digitalRead(Dispatch2BT);

//Set variable for blink counter for blinking lights.
unsigned long currentMillis = millis();
unsigned long currentEstopMillis = millis();

if(floorless == true){
if(RCSEnable == HIGH){
  if(currentEstopMillis - previousEstopAlwaysMillis > EstopBLKFloorless){ 
 previousEstopAlwaysMillis = currentEstopMillis; //Initializes timer with new value
    if (EstopON == LOW){ //If Estop light is off...
      EstopON = HIGH;} //Turn on...
    else //If Estop light is on...
      EstopON = LOW; //Turn off...
    digitalWrite(EstopLT, EstopON);} //Write to Estop light to turn on/off
}
  

//Blinking Dispatch Light Function (Dispatch blinks only when Gates and Restraints are closed)
if(RCSEnable == HIGH){
if (currentMillis - previousMillis > DispatchBLK){
  previousMillis = currentMillis; //Initializes timer with new value
  if (DispatchON == LOW){ //If dispatch light is off...
    DispatchON = HIGH;} //Turn on...
  else //If dispatch light is on...
    DispatchON = LOW; //Turn off...
  digitalWrite(DispatchLT, DispatchON);
  digitalWrite(Dispatch2LT, DispatchON);} //Write to dispatch lights to turn on/off
}

}

}

LightFlashTest.ino (2.13 KB)

TTD03:
while the if statement of the Dispatch and Dispatch2 code is being executed,

Which IF statement is that?

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Also please use the AutoFormat tool to indent the code in a logical fashion so it is easy to see the extent of each block of code.

The names of your variables might make more sense if you explain that the program is for.

...R

First, it'll help you get responses if you put your code inside [code][/code] tags. It makes it easier to display and copy the code.

It was my understanding that no other executing occurs while one if statement is being executed. That being said, while the if statement of the Dispatch and Dispatch2 code is being executed, the Estop code should not be able to be executed, basically making the Estop flash come to a halt until the Dispatch and Dispatch2 if statement is finished, and then the code loops around and the Estop code executes again.

It seems you might be confusing if() statements with some sort of a looping statement. The if() will only execute once and then exit.

Your first if() statement will be true every 100ms (EstopBLKFloorless), at which point it will toggle the state of your EstopLT pin before moving on to the second if() statement. Your second if() statement will be true every 1000ms (DispatchBLK), at which point it will toggle the state of your DispatchLT and Dispatch2LT pins before moving to the end of loop(). At this point, loop() starts all over again because that's what it does.

The if() statements don't have any looping ability themselves. They are simply "execute code block if this statement is true" statements , not "wait until this statement is true and then execute code block" statements.