Shop OBEX P1 Docs P2 Docs Learn Events
A program for an electric piano — Parallax Forums

A program for an electric piano

Daniel CopelandDaniel Copeland Posts: 5
edited 2004-10-28 03:10 in Learn with BlocklyProp
I just wanted to share this program I just wrote. It works really well and would be a fun class project.
The only problem I had with it is the sound from the speaker is not really loud, so any suggestions to make it louder would be appreciated. (For teachers a quieter speaker might be a good thing smilewinkgrin.gif )
·
This idea was an evolution of a speaker controlled by a potentiometer. I discovered that the by setting the program to restrict the tones emitted by the speaker, one could protect their ears from the annoying squeals. With this program you only get tones from the sixth octave, based on the position of the potentiometer.
·
·
'Program written by Daniel Copeland
'Electronic Piano
'Potentiometer values equal between 1 and 656
'Board requires 103 pot, 104 capacitor, 440-ohm resistor, piezo speaker, and four jumpers.
'I used the circuits designs from What's a Microcontroller? On pages 147·and 216.
'All tones in 6th octave.
'{$STAMP BS2}
'{$PBASIC 2.5}
time VAR Word
tone VAR Word
DO
HIGH 7
PAUSE 100
RCTIME 7, 1, time
DEBUG ? time
IF time = 0· THEN
tone = 0
ELSEIF 0 < time < 94 THEN
tone = 1047· 'C6
ELSEIF 93 < time < 187 THEN
tone = 1175· 'D6
ELSEIF 186 < time < 280 THEN
tone = 1319· 'E6
ELSEIF 279 < time < 373 THEN
tone = 1397· 'F6
ELSEIF 372 < time < 466 THEN
tone = 1568· 'G6
ELSEIF 465 < time < 559 THEN
tone = 1760· 'A6
ELSEIF 558 < time < 656 THEN
tone = 1976· 'B6
ENDIF
FREQOUT 9, 500,tone
LOOP
·

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-09-17 23:03
    Let me show you a way to use LOOKDOWN and LOOKUP to avoid those gigantic IF-THEN-ELSEIF constructs:

    ' {$STAMP BS2} 
    ' {$PBASIC 2.5} 
    
    time    VAR    Word 
    tone    VAR    Word 
    
    Main: 
      DO 
        HIGH 7 
        PAUSE 1
        RCTIME 7, 1, time 
        DEBUG ? time 
        LOOKDOWN time, <=[noparse][[/noparse]0, 93, 186, 279, 372, 465, 558, 660], tone 
        LOOKUP tone, [noparse][[/noparse]0, 1047, 1175, 1319, 1397, 1568, 1760, 1976], tone 
        FREQOUT 9, 500, tone 
      LOOP
    


    You don't need 100 ms to charge/discharge the cap for RCTIME.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • Daniel CopelandDaniel Copeland Posts: 5
    edited 2004-09-20 18:07
    Thank you very much, that does shorten it up a bit. Any ideas on how to increase the sound output. There is no resistor inhibiting the speaker so I really dont know what to do.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-09-20 18:19
    Use an amplifier. The Stamp can only drive so much, and the RC filter that converts the Stamp's output to nice sine waves also knocks down the signal a bit.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • Daniel CopelandDaniel Copeland Posts: 5
    edited 2004-09-20 20:04
    Logical step, lol, thank you very much.
  • edited 2004-10-27 04:22
    Daniel,

    Great work!

    I would recommend next borrowing from Chapter 3, Activity #5 to make it so that a pushbutton triggers the note. By all means, post it if you get it up and running.

    For extra volume, bump it up to the 7th octave. Speakers are natural filters, and they all have their favorite broadcast frequency. Aside from their favorite frequency, called the center frequency, they also have a pass-band. This is the range of frequencies the speaker effectively broadcasts without reducing the volume too far (called attenuation). Stereo speakers provide a good example with their woofer, tweeter, and midrange. The woofer has a pass band of low frequencies while the tweeter has a pass band of high frequencies.

    Your piezospeaker is definitely a tweeter, and the 6th octave is still a bit below its pass band. The 7th is mostly in it, as is most of the 8th, which is annoyingly high. Worth a try though.

    Chapter 1 in Basic Analog and Digital and Chapter 8 in Understanding signals have some information to get you started with opamps, and it would be good to try them out before using them in an audio amp circuit. Both of these books are available from Downloads -> Stamps in Class Tutorials.

    Audio speakers have coiled wires that create magnetic fields as electricity passes through them. Since the current changes direction rapidly, so does the magnetic field created by the speaker coil. These magnetic fields act on a magnetic part attached to the speaker paper. As the part vibrates, so does the speaker paper, which in turn vibrates the air pressure, which is what our ears detect and our minds interpret as tones.

    The piezospeaker is not like an audio speaker. What's a Microcontroller, Chapter 8 talks about the piezoelectric element inside these speakers and how it vibrates the air when the voltage applied to it changes rapidly. The difference to remember is this: an audio speaker's element moves because current passes through its coil while a piezospeaker's element moves because voltage changes its shape.

    Because of thse differences, a piezospeaker is not designed to be driven by an audio amplifier. Audio amplifiers push and pull current through a coil. If you amplify a piezospeaker, all you have to do is amplify the voltage, but the current requirements are next to nothing. The LM358 doesn't have nearly as much current capability, but it can amplify voltage, which will amplify your piezospeaker (assuming it's rated for the higher voltage).

    Some speakers that look like piezospeakers are actually designed like audio speakers, but the one in the What's a Microcontroller kit is a true piezo. I do not remember the input voltage spec for the peizospeaker we ship with the What's a Microcontroller kits, and I can't find that out until tomorrow. If it's more than 8 V, there's a cool thing you can try with an LM358 opamp. If it's only 5 V, there's not going to be much difference between driving it with an LM358 and driving it with a BASIC Stamp I/O pin.

    After getting into some BASIC Stamp opamp control, you'll be ready to try out an audio amplifier like the LM386 along with a true audio speaker. I'll post some LM386 resources tomorrow along with a follow-up on input voltages for the piezospeaker in the kit.

    Andy

    Post Edited (Andy Lindsay) : 10/27/2004 4:32:54 AM GMT
  • edited 2004-10-27 04:51
    Here is a third way to convert the pot readings to tone frequencies. SELECT the time variable and evaluate it on a CASE by CASE basis.  
     
    Your approach and this approach both afford you the flexability of performing more than one action based on the time variable.  You can add code for light indicators, displays, etc.  On the other hand, the lookup/lookdown approach takes the least code space, and can come in handy in a pinch.  
    
    
    [size=2][code]
    SELECT time 
    
      CASE <= 1 
        tone = 0 
      CASE < 94 
        tone = 1047 'C6 
      CASE < 187 
        tone = 1175 'D6 
      CASE < 280 
        tone = 1319 'E6 
      CASE < 373 
        tone = 1397 'F6 
      CASE < 466 
        tone = 1568 'G6 
      CASE time < 559 
        tone = 1760 'A6 
      CASE ELSE 'time > 559 
        tone = 1976 'B6 
      ENDSELECT
    

    [/code][/size]
    Post Edited (Andy Lindsay) : 10/27/2004 5:03:15 AM GMT
  • edited 2004-10-28 03:10
    Alrighty, the piezospeaker is rated for 5 V, but it can handle Vpp (peak to peak voltage) of up to 20 V.· So, an amplifier is fair game.· Here is a link to a piezospeaker volume control activity:

    http://forums.parallax.com/showthread.php?p=517152

    If you want to try driving a real speaker, the Parallax Sound AppMod is a good starting point.· On the Sound AppMod's product page, there's also a link to the docs, which has a schematic if you are interested in building a speaker driver from scratch.· Just type "Parallax Sound AppMod" into the search field on the www.parallax.com home page to find the product page.

    Post Edited (Andy Lindsay) : 11/3/2004 2:31:48 AM GMT
Sign In or Register to comment.