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);
}