Making an output go high while another sequence is running

I've written a simple beginner's code that works up to a point but I want to add something else. What works so far is that pressing a button causes 4 OUTPUT pins to go HIGH progressively (not in turn) and all four remain HIGH until another (second) button is pressed.

This is what I want to add: If another (third) button is pressed at any time after the sequence has started but before the last OUTPUT pin goes HIGH, a 5th OUTPUT pin goes HIGH without interfering with the main sequence and all 5 outputs stay HIGH until the second button is pressed.

I've tried inserting "if else" into the sequence but I have so little knowledge of programming that I can't get it to work properly.

I could easily do this by hardwiring an external logic gate to the Arduino pins but I'd like to do this within Arduino itself. Can you please point me in the right direction?

Here's a screenshot of my simulation at Wokwi

And this is the code so far:

void setup()
{
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(13, OUTPUT);
}

void loop() 
{
if (digitalRead(7) == LOW)
{
delay(500);
digitalWrite(2, HIGH);
delay(500);
digitalWrite(3, HIGH);
delay(500);
digitalWrite(4, HIGH);
delay(500);
digitalWrite(5, HIGH);
}

if (digitalRead(8) == LOW)
{
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(3, LOW);
digitalWrite(2, LOW);
digitalWrite(13, LOW);
}
}

The delay() function stops anything happening during the delay period so you cannot read an input to interrupt the sequence during a delay()

You need to use non blocking timing using millis() to do what you want. That will require the sketch to be restructured and a solution cannot just be added to your your existing sketch

Start by looking at Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

Thanks for the quick reply and for the pointers. That's the kind of response I was hoping for.

I'd looked at BlinkWithoutDelay and read about millis, both quite some time ago and not in connection with this project. But I'm such a newbie to programming of any kind that those things didn't enter my mind. I'll look them up again and post again if I have - and probably will have - any questions.

What should happen if the third button is pressed without the sequence having been started by button one?

Attach an interrupt to the new button pin and that way you can add a behavior without interfering with the main sequence.

Nothing. Also nothing if the third button is pressed after the 4th output has gone high.

Ah, yes. Interrupts too. I have only a general idea of what they do but not enough to put them to use. This will be a good time to learn.

Or replace the delay calls with calls to a new function like this:

void delayAndCheckButton3() {
    const auto wakeupTime = millis() + 500;
    while (millis() < wakeupTime) {
        if (digitalRead(9) == LOW) {
            digitalWrite(13, HIGH);
        }
    }
}

In general, you will also need to take care of debouncing the buttons.

Thanks. Will look into that. Actually, the buttons are to be sensors, not physical buttons.

The Arduino is to be at the heart of a fairly complex system. I designed and built a similar setup about 15 years ago without an MCU. It used fixed logic chips, analog devices, several sensors, a dozen SSRs, etc.

That question almost matches this introdution to a very good tutorial:

So is it intended as follows?

Yes, I completely agree with and am well aware of the points made there.

As I mentioned earlier, I'm a total newbie, not only to Arduino but also to any programmable device. OTOH, I've been designing and building electronics stuff, always from scratch (no off-the-shelf modules, no copying of others' designs), for well over 50 years. Why I'm only entering the world of software-driven devices at this late date is a long story.

Anyway, I've seen plenty of hardware products, including those by giant companies, that are ridiculously messed up and could be greatly simplified and optimised with a better approach without sacrificing performance or reliability.

As far as programming is concerned, I'm just learning to take tottering baby steps now. Even so, I've already seen plenty of convoluted codes written by people with far more knowledge and experience that IMHO would benefit from some streamlining. You can be sure that I'll always keep that in mind as I learn.

Here is a variant of yours with an implicit state machine: LED-Special PA - Wokwi ESP32, STM32, Arduino Simulator