Controlling a piezo buzzer and a LED with a push-button.
quotidian
Posts: 5
Aloha,
·
I’m a newbie. I searched the old posts but couldn’t find an answer to my question.
·
I want to control an LED and a Piezo buzzer simultaneously with a push-button. For example, when the button is pressed, the LED lights up and the buzzer makes a sound; when the user lets up on the button, the LED goes off and the buzzer is silent.
·
Here is the code I've been using:
·
DO
·IF (IN8 = 1) THEN 'If the button in Input8 is pressed then . . .
··· HIGH 7 'the LED in Input7 will light up
··· FREQOUT 12, 1000, 2000 'the speaker in Input12 will sound for 1 second (tone 2000)
·
· ELSE
··· LOW 7· 'the LED turns off
·
· ENDIF
·
LOOP
·
As expected, the buzzer sounds for 1 second and then turns off; the LED does the same. I reduced the FREQOUT duration to 100 milliseconds. Although this made the buzzer more responsive to letting up on the push-button, the sound was not adequate.
·
Is there a way to “trick” the buzzer into “following” the commands of the push-button, without it losing its sound quality from the aforementioned code?
·
Any suggestions, sample code, moral support, etc. is greatly appreciated.
·
Thanks in advance.
·
Dean
·
I’m a newbie. I searched the old posts but couldn’t find an answer to my question.
·
I want to control an LED and a Piezo buzzer simultaneously with a push-button. For example, when the button is pressed, the LED lights up and the buzzer makes a sound; when the user lets up on the button, the LED goes off and the buzzer is silent.
·
Here is the code I've been using:
·
DO
·IF (IN8 = 1) THEN 'If the button in Input8 is pressed then . . .
··· HIGH 7 'the LED in Input7 will light up
··· FREQOUT 12, 1000, 2000 'the speaker in Input12 will sound for 1 second (tone 2000)
·
· ELSE
··· LOW 7· 'the LED turns off
·
· ENDIF
·
LOOP
·
As expected, the buzzer sounds for 1 second and then turns off; the LED does the same. I reduced the FREQOUT duration to 100 milliseconds. Although this made the buzzer more responsive to letting up on the push-button, the sound was not adequate.
·
Is there a way to “trick” the buzzer into “following” the commands of the push-button, without it losing its sound quality from the aforementioned code?
·
Any suggestions, sample code, moral support, etc. is greatly appreciated.
·
Thanks in advance.
·
Dean
Comments
·· You probably won't get that kind of responsiveness without using an external buzzer of tone generator triggered by the I/O pin the same way the LED is.· The FREQOUT command requires a duration parameter.· You can't force it off by letting go of the button.· Sorry.· You could pull something like this off using an SX and SX/B though.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
Rafael
Rafael, yes that's what I mean.
Spkr······· PIN··· 12
StartBtn··· PIN··· 8
Led········ PIN··· 7
Setup:
· LOW Spkr
· LOW Led
Main:
· DO
··· Spkr = Spkr·^ StartBtn & StartBtn
··· Led = StartBtn
· LOOP
The first line toggles the speaker output -- but only when the start button is pressed (1).· The next line causes the LED to follow the start button; when start is pressed the LED will be on.
If the tone is too high you can insert short a short delay to extend the loop timing.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Rafael
"If the tone is too high you can insert short a short delay to extend the loop timing." How would I do that?
Thanks, Rafael. Can you post the code for your test program?
Main:
· DO
··· Spkr = Spkr·^ StartBtn & StartBtn
··· Led = StartBtn
··· PAUSE 0
· LOOP
The PAUSE 0 line delays about 150 uSecs or so; this will reduce the tone but shouldn't reduce it too much.· Just understand that you probably can't get an exact pitch with an interpreted language -- you'd need the SX to do that.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Here:
' {$STAMP BS2p}
' {$PBASIC 2.5}
main:
IF IN1=1 THEN
FREQOUT 0,100,100
GOTO main
ELSE
GOTO main
ENDIF
Rafael
Rafael
P.S: The exact output frequency is dependant on the time delay and the given frequency value, so you'll have to play around a bit to find the desired frequency and an acceptable skip.
·· The frequency will also vary slightly depending on what else is running in a loop.· As you add other commands into the loop that will change the frequency some.· Right now the loop is simple.· Just something to keep in mind.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com