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)