How to have a one time event (no loop)

So I have this code running, but what happens is that the event repeats and acts like a blinking lights. What I need to happen is that after the conditions were met, led 1 and led 2 only lits up for 3 seconds then led 3 will lit up for the entire time.

const int buttonPin = 2;     
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int ledPin =  12;      
const int ledPin2 = 11;
const int ledPin3 = 13;

int buttonState = 1;
int buttonState2 = 1;
int buttonState3 = 1;
int buttonState4 = 1;
void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin, INPUT);

void loop() {
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
  if (buttonState == LOW && buttonState2 == LOW && buttonState3 == LOW && buttonState4 == LOW) {
    digitalWrite(ledPin, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin, LOW);
    digitalWrite(ledPin2, LOW);
  else {
    digitalWrite(ledPin, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);

If you really only want it to work once put the code in setup() and leave loop() empty

void loop() {

The other option is to use a variable to record that it has happened - for example

if (jobDone == false) {
   // do whatever needs doing
   jobDone = true;



You need to keep track of what you are doing, and what has been done.
For this, you need some more variables.
In this case i think a variable called event and another called handled would do.
Now if all buttons and handled return 0, you set event to 1.
Then if event is 1, you'll have whatever lights you want lit, to do so.
Now your timer can start.
After the timer has lapsed, you can set the lights you want lit another time, and set handled to 1.

You can easily expand this later with a reset function.

Why can't I make the the first 2 leds turn off after led3 lit up? Are they still following the first if statement?

Because this is in loop and runs over and over again.
If you let go of the button and press it again, what would happen then ?
You put a delay() in there, so this takes about 3.000003 seconds to occur.
If you get rid of the delay as you should, the events would occur thousands of times per second.

There's also a way of blocking anything to happen after the first event is completed.
You'd have to reset the Arduino to get it going again.
Well, actually multiple ways, Robin2 mentioned one, i'm not going to mention the other one as it doesn't help you in any way to understand how things work.

bad programming as this really requires no blocking code and the use of a millis() timer. But if bad code works for you just delete the else

else {

digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);

you will have to reboot to reset the arduino to make the leds go out.

Have a look at how to manage timing (and flashing LEDs) using millis() in the demo several things at a time