Can't run two functions in same time

I have a program with two outputs(LEDs) and two inputs(buttons). If button1 gets High no matter how long, Led1 blinks 2 times and stops. If button2 is HIGH then led2 gets HIGH too. Now I want to make those two functions run in same time and use condition where led1 can blink only if led2 is HIGH, but they won't run in same time. Why? is there a way to make it?

Here is my code:

int led1 = 6;
  int led2 = 9;
  int button1 = 3;
  int button2 = 8;

  int buttonState = 0;
  int lastButtonState =0;
  int flashState = false;
  
void setup() {
  
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  
}

void loop() {
  if(digitalRead(button2) == HIGH){
    digitalWrite(led2, HIGH);
  }else{
    digitalWrite(led2, LOW);
  }

  
  buttonState = digitalRead(button1);
  if(buttonState != lastButtonState)
  {
    if(buttonState == HIGH)
    {
      flashState = true;
    }
  }
  lastButtonState = buttonState;

  if(flashState == true){
    digitalWrite(led1, HIGH);
    delay(500);
    digitalWrite(led1, LOW);
    delay(500);
    digitalWrite(led1, HIGH);
    delay(500);
    digitalWrite(led1, LOW);
    delay(500);
    flashState = false;
  }else{
    digitalWrite(led1, LOW);

  }
}

Why? is there a way to make it?

Eliminate the delay() call. Take a look at the BlinkWithoutDelay example, it shows you how to get what you're looking for.

@Elaz1234

STOP CROSS POSTING !
Any further infraction may be met with a timeout.

Please read this before using the forum any further.

Bob.

ballscrewbob:
@Elaz1234

STOP CROSS POSTING !
Any further infraction may be met with a timeout.

Please read this before using the forum any further.

Bob.

What do you mean? Why are you answering on my old post instead of sending me private message? I'm confused, is cross posting when i post the same topic on different ''questions'' I did so because this might be either programming or electronics question, i couldn't decide so i put on both. Now can i repost it?

Did you read the contents of the link at all ?

Clearly not or you would know that cross posting is frowned on, and would not be asking if you can do it again.

Bob.

TOPIC NOW LOCKED