little annoying project. please help

Break some heads with the software ....
I built a circuit with some LEDs, Button (ButtonPin) and a buzzer (BuzzerPin)
And I wrote a program with which I want to make:

  • Buzzing when the program launches.
  • Button set as interrupt to stop all the action of the buzzer.
  • At the same time there is the act of counting seconds from the beginning of the circuit, which turns on a green LED after 15 seconds.
    ---- My problem is this: Only when I press the button, the buzzer stops and when I leave the button buzzer goes on.
    How do I overcome this and causes buzz completely stop ?!
    I tried many ways but nothing could ...
    Thanks in advance for all helps!

The basic program:

const int ButtonPin=2;
const int BuzzerPin=4;
unsigned long int a=0;
int green=36;
int red=40;
int yellow=44;

void setup()
{
pinMode (yellow,OUTPUT);
pinMode (red,OUTPUT);
pinMode (green,OUTPUT);
pinMode (BuzzerPin,OUTPUT); // sets the pin as output BUZZER
pinMode (ButtonPin,INPUT); // sets the pin as input BUTTON
digitalWrite(red,LOW);
digitalWrite(green,LOW);
digitalWrite(yellow,LOW);
attachInterrupt (ButtonPin, finishbuzzing, HIGH); //if the button pressed (interrupt) jump to 'finishbuzzing'
a=millis();
}

void loop()
{
digitalWrite(red,LOW);
buzzer(); //go to buzzer function
if (millis() - a >= 15000) //chacking for 15 sec
{
digitalWrite(green,HIGH);
a = millis();
}
}

void finishbuzzing()
{
analogWrite(BuzzerPin,LOW); // shut down the buzzer
digitalWrite(red,HIGH); //for indication only
digitalWrite(yellow,LOW);
}

int buzzer()
{
analogWrite(BuzzerPin,128); //start buzzing
digitalWrite(yellow,HIGH); //for indication only
return (a);
}

Use a boolean flag.

Above setup()
bool BuzzFlag = true;

In your loop have this
if(BuzzFlag == true)
buzzer();

In your interrupt function add this
BuzzFlag = false;

Does the buzzer need to reset?

Every time through loop() you call buzzer(). That is why is starts buzzing again. If you move the call to buzzer() to setup() it will not start buzzing again.

i have already tried it.
but the buzzer doesn't work at all (also the yellow indication led)

const int ButtonPin=2;
const int BuzzerPin=4;
unsigned long int a=0;
int green=36;
int red=40;
int yellow=44;
bool b=true;

void setup()
{
pinMode (yellow,OUTPUT);
pinMode (red,OUTPUT);
pinMode (green,OUTPUT);
pinMode (BuzzerPin,OUTPUT); // sets the pin as output BUZZER
pinMode (ButtonPin,INPUT); // sets the pin as input BUTTON
digitalWrite(red,LOW);
digitalWrite(green,LOW);
digitalWrite(yellow,LOW);
attachInterrupt (ButtonPin, finishbuzzing, HIGH); //if the button pressed (interrupt) jump to 'finishbuzzing'
a=millis();
}

void loop()
{
digitalWrite(red,LOW);
if (b==true)
{
buzzer(); //go to buzzer function
}
if (millis() - a >= 15000) //chacking for 15 sec
{
digitalWrite(green,HIGH);
a = millis();
}
}

void finishbuzzing()
{
//analogWrite(BuzzerPin,LOW); // shut down the buzzer
digitalWrite(red,HIGH); //for indication only
digitalWrite(yellow,LOW);
b=false;
}

int buzzer()
{
analogWrite(BuzzerPin,128); //start buzzing
digitalWrite(yellow,HIGH); //for indication only
return (a);
}

your function does not affect "a" so why return it? It is a global variable.

int buzzer()
{
  analogWrite(BuzzerPin,128); //start buzzing
  digitalWrite(yellow,HIGH); //for indication only
  return (a);//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<here... what are you doing?
}

adding to Bulldogs reply, even if it did return a your function return type is significantly smaller than unsigned long

should you not have a statement:
digitalWrite(BuzzerPin,LOW);
in function finishbuzzing(); you have a commented analogWrite in this funcion.
It should perhaps be analogWrite(BuzzerPin,0);
the digitalWrite option is better.

syedamerali:
should you not have a statement:
digitalWrite(BuzzerPin,LOW);
in function finishbuzzing(); you have a commented analogWrite in this funcion.
It should perhaps be analogWrite(BuzzerPin,0);
the digitalWrite option is better.

analogWrite (pin, 0) already calls digitalWrite (pin, LOW), so it doesn't make any difference which you do.

return (a);

sorry, it's just leftovers from other Attempts.

Unless you are studying interrupts or you intend on later using this on a sensor that uses interrupts, you won't need that for this sketch.

I modified yours to do what looks like you may want...

compiles but not tested:

const int buttonPin=2;
const int BuzzerPin=4;
unsigned long a = 0;
int green=36;
int red=40;
int yellow=44;
boolean buzzState = true;
int lastButtonState;

void setup()
{
  pinMode (yellow,OUTPUT);
  pinMode (red,OUTPUT);
  pinMode (green,OUTPUT);
  digitalWrite(red,LOW);
  digitalWrite(green,LOW);
  digitalWrite(yellow,LOW);
  pinMode (BuzzerPin,OUTPUT); // sets the pin as output BUZZER
  pinMode (buttonPin,INPUT); // sets the pin as input BUTTON
  a=millis();
  buzz();
}

void loop()
{
  int buttonState = digitalRead(buttonPin);
  if (buttonState == LOW && lastButtonState == HIGH)
  {
    buzzState = !buzzState;
    if (buzzState)
    {
      buzz();
    }
    else
    {
      finishbuzzing();
    }
  }
  lastButtonState = buttonState;
  liteGreenLed();
}
//
int buzz()
{
  analogWrite(BuzzerPin,128); //start buzzing
  digitalWrite(yellow,HIGH); //for indication only
}
//
void finishbuzzing()
{
  analogWrite(BuzzerPin,0); // shut down the buzzer
  digitalWrite(red,HIGH); //for indication only
  digitalWrite(yellow,LOW);
}
//
void liteGreenLed()
{
  if (millis() - a >= 15000UL)
  {
    digitalWrite(green, HIGH);
  }
}