Question about how sketch code is executed:

I have a sketch that is working great for what I designed it to do. It controls a relay board as well as a small motor to automate a sequence on a machine that I use. Each sequence takes approximately 12 seconds. I have recently added two "photo-interruptors" that I have tested successfully as inputs to my Arduino. However, I am confused as to how to incorporate them into my existing program. Basically I want the input from the photo-interruptors to cause my sequence to halt depending on certain conditions. But how will the Arduino realize that the input state has changed if it is executing through a 12 second sequence?

The right way to do it is to reduce your 12-second sequence into a series of states. Each time through loop() you can check your inputs and check to see if your system should switch to a new state or continue doing what it is doing. This is called a "State Machine" model. It is a way of eliminating the delay() calls that cause your program to be unresponsive.

The example I wrote in the first post of this Thread might help to illustrate the technique.

...R

Appreciate the input johnwasser and Robin2.

I think I have a good grasp on writing separate functions for my program so that I can avoid using delay() in my code. An issue I am having now is with "live-tracking" the time that my photo-interrupters are LOW. I am able to get the time the photo-interrupters are LOW using millis(), but it returns the time once the photo-interrupter goes back HIGH. My goal is to use the photo-interupters to check for errors in my process. If the photo-interrupter is LOW for longer than 5 seconds then that is an indication of an error in my process. Therefore, I need the time that the photo-interrupter is LOW to consistently update, because the photo-interrupter may not ever go HIGH again without human intervention.

Just note the time when the pin goes low. Each time through loop() you can then check to see if the time since then has been more than 5 seconds and act accordingly.

void loop() {
    static unsigned long timeInterrupterWentLow = 0;
.
.
.
    if (digitalRead(interrupterPin) == LOW) {
        if (timeInterrupterWentLow == 0)
            timeInterrupterWentLow = millis();
        if (millis() - timeInterrupterWentLow > 5UL*60UL*1000UL) {
           // Do Whatever You Need To Do When This Fault Occurs
        }
    } else {
        timeInterrupterWentLow = 0;
    }
.
.
.
}

Awesome! Thanks for the assistance johnwasser. Got me where I wanted to be.

johnwasser:

        if (millis() - timeInterrupterWentLow > 5UL*60UL*1000UL) {

Get rid of that 60UL. 5 seconds, not 5 minutes.