Electronics Projects: How to Use NAND Gates to Create Other Gates
Electronics Projects: Charging and Discharging a Capacitor
Electronics Projects: Materials for a Crystal Radio Circuit

Electronics Projects: How to Program Music on a BASIC Stamp

With your BASIC Stamp processor, PBASIC commands and a piezo speaker, you can make your electronics projects sing. Here is a sample program that plays two songs on a piezo speaker: "Mary Had a Little Lamb" and the Happy Birthday song. The former is played when you press SW1; the latter when you press SW2.

To simplify the code that generates the musical notes, the program defines several constants that represent the frequency for each of the notes required by the songs. For example, the constant NoteC6 is 1046, the frequency in Hz of C in the sixth octave of a piano keyboard.

The constants span two full octaves, which is plenty of range for the songs to be played. Both songs are played in the key of C, so no flats or sharps are required.

The program also sets up constants for the duration of a quarter note, half note, and whole note. The constants make it easy to specify a particular pitch for a particular duration in a FREQOUT command.

Thus, playing a melody is simply a matter of writing a sequence of FREQOUT commands to play the correct notes for the correct durations in the correct order. That’s precisely what the subroutines labeled Mary and Birthday do.

' Song Program
' Doug Lowe
' July 15, 2011
'
' This program plays one of two songs on the piezo speaker
' on pin 0.
' If SW1 on pin 14 is pressed, the program plays "Mary Had a Little Lamb."
' If SW2 on pin 10 is pressed, the program plays "Good Morning to All."
' {$STAMP BS2}
' {$PBASIC 2.5}
SW1    PIN 14
SW2    PIN 10
Speaker  PIN 0
NoteC6  CON 1046
NoteD6  CON 1175
NoteE6  CON 1318
NoteF6  CON 1370
NoteG6  CON 1568
NoteA6  CON 1760
NoteB6  CON 1975
NoteC7  CON 2093
NoteD7  CON 2349
NoteE7  CON 2637
NoteF7  CON 2794
NoteG7  CON 3136
NoteA7  CON 3520
NoteB7  CON 3951
NoteC8  CON 4186
Whole   CON 1000
Half   CON 500
Quarter  CON 250
DO
 IF SW1 = 1 THEN
  GOSUB Mary
 ENDIF
 IF SW2 = 1 THEN
  GOSUB Morning
 ENDIF
LOOP
Mary:
 FREQOUT Speaker, Quarter, NoteE7 ' Mar-
 FREQOUT Speaker, Quarter, NoteD7 ' y
 FREQOUT Speaker, Quarter, NoteC7 ' Had
 FREQOUT Speaker, Quarter, NoteD7 ' a
 FREQOUT Speaker, Quarter, NoteE7 ' Lit-
 FREQOUT Speaker, Quarter, NoteE7 ' tle
 FREQOUT Speaker, Quarter, NoteE7 ' Lamb
 PAUSE Quarter
 FREQOUT Speaker, Quarter, NoteD7 ' Lit-
 FREQOUT Speaker, Quarter, NoteD7 ' tle
 FREQOUT Speaker, Quarter, NoteD7 ' Lamb
 PAUSE Quarter
 FREQOUT Speaker, Quarter, NoteE7 ' Lit-
 FREQOUT Speaker, Quarter, NoteG7 ' tle
 FREQOUT Speaker, Quarter, NoteG7 ' Lamb
 PAUSE Quarter
 FREQOUT Speaker, Quarter, NoteE7 ' Mar-
 FREQOUT Speaker, Quarter, NoteD7 ' y
 FREQOUT Speaker, Quarter, NoteC7 ' Had
 FREQOUT Speaker, Quarter, NoteD7 ' a
 FREQOUT Speaker, Quarter, NoteE7 ' Lit-
 FREQOUT Speaker, Quarter, NoteE7 ' tle
 FREQOUT Speaker, Quarter, NoteE7 ' Lamb
 FREQOUT Speaker, Quarter, NoteE7 ' Its
 FREQOUT Speaker, Quarter, NoteD7 ' Fleece
 FREQOUT Speaker, Quarter, NoteD7 ' Was
 FREQOUT Speaker, Quarter, NoteE7 ' White
 FREQOUT Speaker, Quarter, NoteD7 ' As
 FREQOUT Speaker, Quarter, NoteC7 ' Snow
 PAUSE Half
 RETURN
Morning:
 FREQOUT Speaker, Half, NoteC7   ' Good
 FREQOUT Speaker, Half, NoteD7   ' Morn-
 FREQOUT Speaker, Half, NoteC7   ' ing
 FREQOUT Speaker, Half, NoteF7   ' To
 FREQOUT Speaker, Whole, NoteE7  ' You
 FREQOUT Speaker, Half, NoteC7   ' Good
 FREQOUT Speaker, Half, NoteD7   ' Morn-
 FREQOUT Speaker, Half, NoteC7   ' ing
 FREQOUT Speaker, Half, NoteG7   ' To
 FREQOUT Speaker, Whole, NoteF7  ' You
 FREQOUT Speaker, Half, NoteC7   ' Good
 FREQOUT Speaker, Half, NoteC8   ' Morn-
 FREQOUT Speaker, Half, NoteA7   ' ing
 FREQOUT Speaker, Half, NoteF7   ' Dear
 FREQOUT Speaker, Half, NoteE7   ' Child-
 FREQOUT Speaker, Whole, NoteD7  ' ren
 FREQOUT Speaker, Half, NoteB7   ' Good
 FREQOUT Speaker, Half, NoteA7   ' Morn-
 FREQOUT Speaker, Half, NoteF7   ' ing
 FREQOUT Speaker, Half, NoteG7   ' To
 FREQOUT Speaker, Whole, NoteF7  ' All
 RETURN
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Electronics Projects: How to Use a Piezo Speaker with a BASIC Stamp
How to Complete a Project 2013 Task in Less Time
Electronics Projects: How to Assign Names to I/O Pins in PBASIC Code
Electronics Projects: How to Build Series and Parallel Circuits
Electronics Basics: What is a Gated Latch
Advertisement

Inside Dummies.com