Shop OBEX P1 Docs P2 Docs Learn Events
Controlling a piezo buzzer and a LED with a push-button. — Parallax Forums

Controlling a piezo buzzer and a LED with a push-button.

quotidianquotidian Posts: 5
edited 2005-08-26 16:10 in BASIC Stamp
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

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-08-26 01:35
    Dean,

    ·· 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
  • FlyingFishFingerFlyingFishFinger Posts: 461
    edited 2005-08-26 01:37
    Do you mean to light the LED and sound the buzzer for as long as the button is pressed ?
    Rafael
  • quotidianquotidian Posts: 5
    edited 2005-08-26 01:47
    Thanks, Chris.

    Rafael, yes that's what I mean.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-08-26 02:25
    There will always be a bit of a pop between iterations of FREQOUT because it takes time to load instructions -- you'll never get a nice smooth tone.· You might try something like this:

    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
  • FlyingFishFingerFlyingFishFinger Posts: 461
    edited 2005-08-26 02:31
    You could put the IF statement in a loop with the freqout and use a short period ~100ms maybe, that would be the minimum tone length you can get (Each loop it checks the INx and activates the sound for the short time if IN=1, then loops). I wrote a test program and it kind of works
    Rafael
  • quotidianquotidian Posts: 5
    edited 2005-08-26 03:24
    Thanks, Jon! That works exactly the way I wanted it to. One more question regarding tone control:

    "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?
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-08-26 12:01
    The simplest way is with PAUSE -- but bit numbers will make the frequency go low in a hurry.· Try this first:

    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
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-08-26 12:05
    Jon Williams (Parallax) said...


    Main:
    · DO
    ··· Spkr = Spkr·^ StartBtn & StartBtn
    ··· Led = StartBtn
    ··· PAUSE 0
    · LOOP
    Neat trick, always cool to see an out of the box approach.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-08-26 12:22
    That one comes from our pal Dr. Tracy Allen

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • FlyingFishFingerFlyingFishFinger Posts: 461
    edited 2005-08-26 15:16
    Hello! My code works if you're not critical about minimum tone time and the slight skipping every so many seconds/miliseconds(time delay in freqout) that appears each time you loop.

    Here:

    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    main:
    IF IN1=1 THEN
    FREQOUT 0,100,100
    GOTO main
    ELSE
    GOTO main
    ENDIF

    Rafael
  • FlyingFishFingerFlyingFishFinger Posts: 461
    edited 2005-08-26 15:20
    And you can insert the high/low statement for your LED: HIGH(pin) between "IF IN1=1"... and "FREQOUT...", the LOW(pin) between "FREQOUT..." and "GOTO main"

    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.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-08-26 16:10
    Hello,

    ·· 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
Sign In or Register to comment.