When the Arduino button is continuously pressed, the LED glitters every 0.6 seconds and the buzzer buzzes once

(Please understand that the sentence may sound unnatural using a translator.)
When I keep pressing Arduino's button, I want the buzzer to buzz once and the led to blink every 0.6 seconds.
But the code I wrote doesn't buzzed the buzzer. There may be many errors.

<
int btnPin = 2;
int ledPin = 13;
int btlPin = 4;
int bluePin = 12;
int buzzer = 3;
bool buzzerFlag = true;

void setup() {
pinMode(btnPin , INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
pinMode(btlPin , INPUT_PULLUP);
pinMode(bluePin, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(A0,INPUT);

}
void loop() {
int btnState = digitalRead(btnPin);
int btlState = digitalRead(btlPin);
if (buzzerFlag == true && btnState == HIGH) {
tone(buzzer, 700, 2000); delay(150);
digitalWrite (ledPin, LOW);
delay(600);
digitalWrite (ledPin, HIGH);
delay(500);
}else{
noTone(buzzer);
}
if (btnState == LOW) {
digitalWrite(ledPin, HIGH);
}
if (btlState == HIGH) {
digitalWrite (bluePin, LOW);
delay(600);
digitalWrite (bluePin, HIGH);
delay(500);
}
if (btlState == LOW) {
digitalWrite(bluePin, HIGH);
}}

Do you have any simpler code you can use to prove to yourself that the buzzer works?

Hello iris-002
Post your sketch, well formated, with comments and in so called code tags "</>" and schematic to see how we can help.
Have a nice day and enjoy coding in C++.

By this statement I assume the LED is blinking when you hold the button down. This means the buzzer is not working so you need to check your wiring. Write a simple program just to test the buzzer.

The buzzer works but keeps buzzing until the button is not pressed.

That is because you define buzzerFlag and set it to true. Every time loop() gets called you execute the following conditional:

  if (buzzerFlag == true && btnState == HIGH)
  {
    tone(buzzer, 700, 2000); delay(150);
    digitalWrite (ledPin, LOW);
    delay(600);
    digitalWrite (ledPin, HIGH);
    delay(500);
  } else
  {
    noTone(buzzer);
  }

The tone() function gets called over and over again if buzzerFlag is true and btnState is HIGH. You never set buzzerFlag to false anywhere in your code.

What confuses me is that you have your button inputs set to INPUT_PULLUP so a "button pressed" condition is usually LOW for most applications. Are you using some kind of normally closed button?

Maybe, yes.

Can you describe exactly what you want the program to do? I'm not sure I understand if you want the LEDs to blink WHILE you have the button pressed or in RESPONSE to a button press.

I want to make it when I keep pressing the button, the buzzer buzzes once for a second, and the LED keeps blinking until I don't press the button.

Hello iris-002
I´ve made a code review and the text editor has found five calls of the delay() function.

Line 21:     tone(buzzer, 700, 2000); delay(150);
	Line 23:     delay(600);
	Line 25:     delay(500);
	Line 34:     delay(600);
	Line 36:     delay(500);

Each call will block the execution of the sketch for this time.
My proposal:
At least the sketch needs two timers to get the described functionality without call of delay().
Timer1: blinking Led
Timer2: timed buzzer
These timers are controlled by the bntPin and btlPin keys.
As base for these timers you can use the IDE example BlinkWithoutDelay as mother of all Adruino timers. Get started.
Have a nice day and enjoy coding in C++.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.