A program for an electric piano
Daniel Copeland
Posts: 5
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 )
·
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
·
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 )
·
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
You don't need 100 ms to charge/discharge the cap for RCTIME.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
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
[/code][/size]
Post Edited (Andy Lindsay) : 10/27/2004 5:03:15 AM GMT
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