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