button monitor and action at once

Attached code's purpose want to let the sketch keep a beep intermittently, and monitor the button if pressed do some thing /three(); / at once. But the test shown the action happened till the delay period finished.

Need help how to let the action happen at once when button pressed.

Thanks.

int buzz = 2; // Buzzer Pin
int input = 0;

const int buttonPin = A0;
int buttonState = 0;         // variable for reading the pushbutton status

int j = 3; // number
int k = 12; // number

int period = 10000;
unsigned long time_now = 0;


void setup() {

  for (int i = 2; i <= 13; i++)
    pinMode(i, OUTPUT);

  for (int i = 2; i <= 13; i++)
    digitalWrite(i, LOW);

  pinMode(A0, INPUT);
  digitalWrite(A0, HIGH);
}

// the loop routine runs over and over again forever:
void loop() {

  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == LOW) {

    three();  //// twoA better

  } else {

    for (int i = 2; i <= 13; i++)
      digitalWrite(i, LOW);

    tone(2, 250, 20);
     delay(4000);
   
    }
  }

void three() {              //This function produces the 3rd siren(AMBULANCE) sound with led transition.
  tone(buzz, 440, 200);
  delay(300);
  for (int i = 3; i <= 6; i++)
    digitalWrite(i, HIGH);
  noTone(buzz);
  tone(buzz, 494, 500);
  delay(300);
  for (int i = 3; i <= 6; i++) {
    digitalWrite(i, LOW);
    digitalWrite(i + 6, HIGH);
  }
  noTone(buzz);
  tone(buzz, 523, 300);
  delay(200);
  digitalWrite(7, HIGH);
  delay(50);
  digitalWrite(8, HIGH);
  delay(50);
  noTone(buzz);
}

Don't use delay.

Do timing using the techniques shown in the BlinkWithoutDelay example and you need to program using a Finite State Machine structure to work around the do/wait/do/wait elements of the code.

I recently wrote this blog post that may help you Finite State Machine Programming Basics – Part 1 – Arduino++, but there is lots out there on what to do.

Others may chip in with their own examples.

marco_c:
Don't use delay.

Do timing using the techniques shown in the BlinkWithoutDelay example and you need to program using a Finite State Machine structure to work around the do/wait/do/wait elements of the code.

I recently wrote this blog post that may help you Finite State Machine Programming Basics – Part 1 – Arduino++, but there is lots out there on what to do.

Others may chip in with their own examples.

Thank you.
I checked the BlinkWithoutDelay example which is suitable my case, but it's led ON and OFF at same period, how can modify it ON for 1 second and OFF for 5 seconds?

Keep the delay target time in a variable. When the LED is turned on, set the variable to 1 second. When the LED is turned off, set the variable to 5 seconds.

if you are looking at the blink without delay example. everything in the bracket happens once every second. if you want to have something change every 5 seconds use a timer.

int timer;
unsigned long last;

void loop() {
if (millis() - last >=1000) {
  last = millis();

 timer++;
if(timer==5){timer=0;}

    // if the LED is off turn it on and vice-versa:
    if (timer==0){
     digitalWrite(ledPin,HIGH);
    } else {
     digitalWrite(ledPin, LOW);
    }


  }}