Hello,
I got my KY-006 passive buzzer in the mail today and wanted to try to interact it with my LEDs. I have a code that I got some great help on to make my LEDs work well, so what I want to do now is simply have the buzzer turn on whenever any one of the three LEDs lights up, and stay off when all the LEDs are off. All the code examples I've seen with this particular buzzer have included delay and obviously I don't want to have that in this kind of project. Can anyone help me with this project? Thank you very much. Here is my existing code:
//+5 turns LED on
#define LEDon HIGH
#define LEDoff LOW
//+5 turns LED off
//#define LEDon LOW
//#define LEDoff HIGH
//=========================================
const unsigned long MyTime12 = 5*1000ul;
unsigned long Millis12;
const byte ledPin12 = 12;
const byte buttonPin2 = 2; //pushed = LOW
boolean flag12 = false;
unsigned long MyTime13;
unsigned long Millis13;
const byte ledPin13 = 13;
const byte buttonPin3 = 3; //pushed = LOW
boolean flag13 = false;
byte myState = 0;
const unsigned long MyTime11 = 5*1000ul;
unsigned long Millis11;
const byte ledPin11 = 11;
const byte buttonPin4 = 4; //pushed = LOW
boolean flag11 = false;
//=========================================
//variable for reading the pushbutton status
int buttonState = 0;
void setup()
{
pinMode(ledPin12, OUTPUT);
//LED off at power on
digitalWrite(ledPin12, LEDoff);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(ledPin11, OUTPUT);
//LED off at power on
digitalWrite(ledPin11, LEDoff);
pinMode(buttonPin4, INPUT_PULLUP);
pinMode(ledPin13, OUTPUT);
//LED off at RESET
digitalWrite(ledPin13, LEDoff);
pinMode(buttonPin3, INPUT_PULLUP);
}
void loop()
{
//=========================================
// read the state of the push button value:
buttonState = digitalRead(buttonPin2);
//LOW is pressed
//if button pressed, start timing LED sequence
if (flag12 == false && buttonState == HIGH)
{
//enable timing
flag12 = true;
Millis12 = millis();
//LED on
digitalWrite(ledPin12, LEDon);
}
// read the state of the push button value:
buttonState = digitalRead(buttonPin4);
//LOW is pressed
//if button pressed, start timing LED sequence
if (flag11 == false && buttonState == HIGH)
{
//enable timing
flag11 = true;
Millis11 = millis();
//LED on
digitalWrite(ledPin11, LEDon);
}
//=========================================
// read the state of the push button value
buttonState = digitalRead(buttonPin3);
//LOW is pushed
//if button pressed, start LED timing sequence
if (flag13 == false && buttonState == HIGH)
{
//enable timing
flag13 = true;
myState = 0;
Millis13 = millis();
MyTime13 = 180*1000UL; //LED OFF for 180 seconds 180*1000
digitalWrite(ledPin13, LEDoff);
//LED will remain OFF for 180 more seconds before it comes ON for 10 sec
}
//=========================================
//Time to toggle LED?
if(flag12 == true && millis() - Millis12 >= MyTime12)
{ //toggle ledPin12
digitalWrite(ledPin12, !digitalRead(ledPin12));
flag12 = false;
}
//Time to toggle LED?
if(flag11 == true && millis() - Millis11 >= MyTime11)
{ //toggle ledPin11
digitalWrite(ledPin11, !digitalRead(ledPin11));
flag11 = false;
}
//=========================================
//Time to toggle LED?
if(flag13 == true && millis() - Millis13 >= MyTime13)
{
//get ready for next iteration
Millis13 += MyTime13;
switch(myState)
{
case 0:
digitalWrite(ledPin13, LEDon);
myState = 1;
MyTime13 = 10*1000UL; //LED ON for 10 seconds
break;
case 1:
//we are now finished with this button press
digitalWrite(ledPin13, LEDoff);
flag13 = false;
break;
}//End of switch
}
} //END of loop()
I appreciate the advice. It still is rather new to me trying to figure out things, even relatively simple things for experienced people. I changed a bit of the code basically to test the buzzer to see if it would work. When I press the desired button the LED still works as normal, but there is no sound coming from the buzzer. It is a passive buzzer, if that helps to mention. Could you maybe show me how to code one part of this in relation to the LEDs, and maybe I could figure the rest out from there? Thanks again. Here is the updated code:
//+5 turns LED on
#define LEDon HIGH
#define LEDoff LOW
#define Buzzeron HIGH
#define Buzzeroff LOW
//+5 turns LED off
//#define LEDon LOW
//#define LEDoff HIGH
//=========================================
const unsigned long MyTime12 = 5*1000ul;
unsigned long Millis12;
const byte ledPin12 = 12;
const byte buttonPin2 = 2; //pushed = LOW
boolean flag12 = false;
unsigned long MyTime13;
unsigned long Millis13;
const byte ledPin13 = 13;
const byte buttonPin3 = 3; //pushed = LOW
boolean flag13 = false;
byte myState = 0;
const unsigned long MyTime11 = 5*1000ul;
unsigned long Millis11;
const byte ledPin11 = 11;
const byte buttonPin4 = 4; //pushed = LOW
boolean flag11 = false;
const byte buzzer = 8;
//=========================================
//variable for reading the pushbutton status
int buttonState = 0;
void setup()
{
pinMode(ledPin12, OUTPUT);
//LED off at power on
digitalWrite(ledPin12, LEDoff);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(ledPin11, OUTPUT);
//LED off at power on
digitalWrite(ledPin11, LEDoff);
pinMode(buttonPin4, INPUT_PULLUP);
pinMode(ledPin13, OUTPUT);
//LED off at RESET
digitalWrite(ledPin13, LEDoff);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode (buzzer, OUTPUT);
digitalWrite(buzzer, Buzzeroff);
}
void loop()
{
//=========================================
// read the state of the push button value:
buttonState = digitalRead(buttonPin2);
//LOW is pressed
//if button pressed, start timing LED sequence
if (flag12 == false && buttonState == HIGH)
{
//enable timing
flag12 = true;
Millis12 = millis();
//LED on
digitalWrite(ledPin12, LEDon);
digitalWrite(buzzer, Buzzeron);
}
// read the state of the push button value:
buttonState = digitalRead(buttonPin4);
//LOW is pressed
//if button pressed, start timing LED sequence
if (flag11 == false && buttonState == HIGH)
{
//enable timing
flag11 = true;
Millis11 = millis();
//LED on
digitalWrite(ledPin11, LEDon);
}
//=========================================
// read the state of the push button value
buttonState = digitalRead(buttonPin3);
//LOW is pushed
//if button pressed, start LED timing sequence
if (flag13 == false && buttonState == HIGH)
{
//enable timing
flag13 = true;
myState = 0;
Millis13 = millis();
MyTime13 = 180*1000UL; //LED OFF for 180 seconds 180*1000
digitalWrite(ledPin13, LEDoff);
//LED will remain OFF for 180 more seconds before it comes ON for 10 sec
}
//=========================================
//Time to toggle LED?
if(flag12 == true && millis() - Millis12 >= MyTime12)
{ //toggle ledPin12
digitalWrite(ledPin12, !digitalRead(ledPin12));
flag12 = false;
}
//Time to toggle LED?
if(flag11 == true && millis() - Millis11 >= MyTime11)
{ //toggle ledPin11
digitalWrite(ledPin11, !digitalRead(ledPin11));
flag11 = false;
}
//=========================================
//Time to toggle LED?
if(flag13 == true && millis() - Millis13 >= MyTime13)
{
//get ready for next iteration
Millis13 += MyTime13;
switch(myState)
{
case 0:
digitalWrite(ledPin13, LEDon);
myState = 1;
MyTime13 = 10*1000UL; //LED ON for 10 seconds
break;
case 1:
//we are now finished with this button press
digitalWrite(ledPin13, LEDoff);
flag13 = false;
break;
}//End of switch
}
} //END of loop()
A passive buzzer needs an AC signal to drive it.
I found this example sketch, and the buzzer works just fine, so I'm still curious as to why it won't buzz while an LED is on in that other sketch.
int buzzer = 8 ;// setting controls the digital IO foot buzzer
void setup ()
{
pinMode (buzzer, OUTPUT) ;// set the digital IO pin mode, OUTPUT out of Wen
}
void loop ()
{
unsigned char i, j ;// define variables
while (1)
{
for (i = 0; i <80; i++) // Wen a frequency sound
{
digitalWrite (buzzer, HIGH) ;// send voice
delay (1) ;// Delay 1ms
digitalWrite (buzzer, LOW) ;// do not send voice
delay (1) ;// delay ms
}
for (i = 0; i <100; i++) // Wen Qie out another frequency sound
{
digitalWrite (buzzer, HIGH) ;// send voice
delay (2) ;// delay 2ms
digitalWrite (buzzer, LOW) ;// do not send voice
delay (2) ;// delay 2ms
}
} }
GoBraves21:
I found this example sketch, and the buzzer works just fine, so I'm still curious as to why it won't buzz while an LED is on in that other sketch.
int buzzer = 8 ;// setting controls the digital IO foot buzzer
void setup ()
{
pinMode (buzzer, OUTPUT) ;// set the digital IO pin mode, OUTPUT out of Wen
}
void loop ()
{
unsigned char i, j ;// define variables
while (1)
{
for (i = 0; i <80; i++) // Wen a frequency sound
{
digitalWrite (buzzer, HIGH) ;// send voice
delay (1) ;// Delay 1ms
digitalWrite (buzzer, LOW) ;// do not send voice
delay (1) ;// delay ms
}
for (i = 0; i <100; i++) // Wen Qie out another frequency sound
{
digitalWrite (buzzer, HIGH) ;// send voice
delay (2) ;// delay 2ms
digitalWrite (buzzer, LOW) ;// do not send voice
delay (2) ;// delay 2ms
}
} }
Because that sketch sends an AC signal to the buzzer, and yours doesn't. Try using the tone() function.
Thanks again aarg. I got what you meant now. I tested it with this and it works well: tone(buzzer, 400, 1500).