teaching project: sound/music with PBASIC/BOE Bot/piezospeaker
                    So.
This is a several-part question. Any help is welcome, 'cause I'm struggling here. Background is, I'm trying to teach some high school kids about programming, using the BOE Bot/Board of Education kit. I don't know a lot about this stuff: I've done some other programming, but I'm brand new to PBASIC, and I know very little about electronics/robotics.
I am trying to get them at least a little bit excited by making the BOE Bot play the Overworld Theme from the Legend of Zelda (I also don't know how to read music . . . yet). I'm afraid I resorted to taking requests. I'll include more details on this below.
#1. Any general advice on playing music this way? You may want to read the code I include below before answering.
#1.1: Can I use multiple cogs to play multiple notes simultaneously?
#1.1.1: I do have 8 cogs, on the BOE Bot Board of Education, right?
#1.1.2: I am using a piezospeaker. Do I need one piezospeaker for each note I want to play simultaneously, assuming that's possible?
#2: Any suggestions of other, favorite starter projects that will get kids (Jr High/HS) excited?
#3: Can I write sets (in some form) of music notes into arrays, and iterate through them with, e.g. a FOR . . . NEXT loop?
#3.1: Can I use the length of the array as the termination value for that loop?
#3.2: I think I read that I can't nest arrays in PBASIC. Is there a good way to simulate nested arrays, or is that not worth the trouble?
#4: I'm interested in eventually writing a program that the kids (and I) can use to "plug in" sets of notes (or sets of measures) to have the STAMP play through one or more piezospeakers. Any general (or specific) suggestions on how to do this?
#5: In terms of translating beats per minute into milliseconds, one "beat" should equal one "whole note," right? Again, I don't know anything about music, really...
#6. Does PBASIC have a way to "include" other files? So that I could write this file as a frame, then write the actual notes to play, and the beats per minute, in another file?
#7. Is there something in PBASIC like the 'functions' or 'methods' I'm used to from other languages, to which you can pass variables for use within the [function/method]?
Here's the code I have so far:
                            This is a several-part question. Any help is welcome, 'cause I'm struggling here. Background is, I'm trying to teach some high school kids about programming, using the BOE Bot/Board of Education kit. I don't know a lot about this stuff: I've done some other programming, but I'm brand new to PBASIC, and I know very little about electronics/robotics.
I am trying to get them at least a little bit excited by making the BOE Bot play the Overworld Theme from the Legend of Zelda (I also don't know how to read music . . . yet). I'm afraid I resorted to taking requests. I'll include more details on this below.
#1. Any general advice on playing music this way? You may want to read the code I include below before answering.
#1.1: Can I use multiple cogs to play multiple notes simultaneously?
#1.1.1: I do have 8 cogs, on the BOE Bot Board of Education, right?
#1.1.2: I am using a piezospeaker. Do I need one piezospeaker for each note I want to play simultaneously, assuming that's possible?
#2: Any suggestions of other, favorite starter projects that will get kids (Jr High/HS) excited?
#3: Can I write sets (in some form) of music notes into arrays, and iterate through them with, e.g. a FOR . . . NEXT loop?
#3.1: Can I use the length of the array as the termination value for that loop?
#3.2: I think I read that I can't nest arrays in PBASIC. Is there a good way to simulate nested arrays, or is that not worth the trouble?
#4: I'm interested in eventually writing a program that the kids (and I) can use to "plug in" sets of notes (or sets of measures) to have the STAMP play through one or more piezospeakers. Any general (or specific) suggestions on how to do this?
#5: In terms of translating beats per minute into milliseconds, one "beat" should equal one "whole note," right? Again, I don't know anything about music, really...
#6. Does PBASIC have a way to "include" other files? So that I could write this file as a frame, then write the actual notes to play, and the beats per minute, in another file?
#7. Is there something in PBASIC like the 'functions' or 'methods' I'm used to from other languages, to which you can pass variables for use within the [function/method]?
Here's the code I have so far:
' {$STAMP BS2}
' {$PBASIC 2.5}
' -----[ Title ]------------------------------
' Piezospeaker plays Legend of Zelda (Overworld Theme)
' (Composed by Koji Kondo)
' -----[ REFERENCES ]-------------------------
'Notes by frequency: http://www.phy.mtu.edu/~suits/notefreqs.html
'"Mo' midi PDF :     http://arts.ufl.edu/music/composition/downloads/nv95.pdf
' -----[ Variables/Constants/Pins ]-----------
Piezo   PIN   4               ' Speaker
C6  CON    1047               ' Piano notes / frequencies
D6  CON    1175
E6  CON    1319
F6  CON    1397
G6  CON    1568
A6  CON    1760
Be6 CON    1976
C7  CON    2093
D7  CON    2349
E7  CON    2637
F7  CON    2793
G7  CON    3136
A7  CON    3520
Be7 CON    3951
Tempo CON 152                 'CHANGEABLE
Wn VAR  Word 'full note
Hn VAR  Word
Qn VAR  Word
En VAR  Word
Sn VAR  Word
Wn = 1000/(Tempo/60)
Hn = Wn/2            'half note
Qn = Hn/2            'quarter note
En = Qn/2            'eighh note
Sn = Sn/2            'sixteenth note
'  This is how I would play a series of quarter notes:
FREQOUT Piezo, Qn, Be7
FREQOUT Piezo, Qn, A7
FREQOUT Piezo, Qn, G7
FREQOUT Piezo, Qn, F7
FREQOUT Piezo, Qn, E7
FREQOUT Piezo, Qn, D7
FREQOUT Piezo, Qn, C7
                
                            
Comments
********************************
#1. Any general advice on playing music this way? You may want to read the code I include below before answering.
YES! Take a look at "What's a Microcontroller?" It is the entry-level book for PBASIC and BASIC Stamp programming. There is an entire chapter on making music with a piezo speaker, with lots of different programming strategies. It is available as a download linked from this page, along with the Micro Music with RTTTL example code: http://www.parallax.com/product/28123
#1.1: Can I use multiple cogs to play multiple notes simultaneously?
NO - see below.
#1.1.1: I do have 8 cogs, on the BOE Bot Board of Education, right?
NO, you do not! The code you show below is PBASIC, for the BASIC Stamp Board of Education and Boe-Bot robot. The BASIC Stamp 2 Microcontroller module is a single-core device. The Propeller microcontroller is the one that has 8 cores. Parallax supports its native Spin and Assembly languages, and also Propeller C in the Propeller C Tutorials series (http://learn.parallax.com/propeller-c-tutorials). There is a Propeller Board of Education product, similar to the BASIC Stamp based Board of Education in its shape and purpose. There is also a Propeller Activity Board, used on the ActivityBot, a robot kit similar to, but far more powerful than, the original Boe-Bot robot.
#1.1.2: I am using a piezospeaker. Do I need one piezospeaker for each note I want to play simultaneously, assuming that's possible?
The BASIC Stamp can only do one thing at a time. So, you could not beep two speakers on two pins at the same time. However, you CAN play two notes on one piezospeaker with the FREQOUT command, just add the optional second freq argument: FREQOUT pin, duration, freq1, freq2.
#2: Any suggestions of other, favorite starter projects that will get kids (Jr High/HS) excited?
We have many! http://learn.parallax.com/projects/basic-stamp-2 I recommend a Bi-color LED Memory Game, Tilt Tones, Build your Own Mini Timer, and Playing Sheet Music with the Piezospeaker. If your students are new to building circuits on a bread board, start them with the Breadboard Basics video: http://learn.parallax.com/reference/breadboard-basics .
#3: Can I write sets (in some form) of music notes into arrays, and iterate through them with, e.g. a FOR . . . NEXT loop?
Take a look at Chapter 8, Activity 3 in What's a Microcontroller v3.0, (page 253)
#3.1: Can I use the length of the array as the termination value for that loop?
Not sure we did that, but another approach is shown in page 261.
#3.2: I think I read that I can't nest arrays in PBASIC. Is there a good way to simulate nested arrays, or is that not worth the trouble?
That's a bit beyond my expertise, but hopefully the techniques offered will get the job done.
#4: I'm interested in eventually writing a program that the kids (and I) can use to "plug in" sets of notes (or sets of measures) to have the STAMP play through one or more piezospeakers. Any general (or specific) suggestions on how to do this?
Chapter 8 has lots of approaches, the best one to use depends on your audience.
#5: In terms of translating beats per minute into milliseconds, one "beat" should equal one "whole note," right? Again, I don't know anything about music, really...
The Playing Sheet Music project mentioned above, and the example program NotesAndDurations.bs2 in Chapter 8, Activity 4 of WAM (page 258) should have you covered.
#6. Does PBASIC have a way to "include" other files? So that I could write this file as a frame, then write the actual notes to play, and the beats per minute, in another file?
Not that I know of for the BASIC Stamp 2.
#7. Is there something in PBASIC like the 'functions' or 'methods' I'm used to from other languages, to which you can pass variables for use within the [function/method]?
Not exactly. You can create subroutines that are called conditionally or within infinite or counted loops, and re-define variable values that these subroutines use prior to or within the subroutine call. This strategy is used in Robotics with the Boe-Bot for sensor-based navigation; the text and the Boe-Bot Light Sensors BS2 example code are linked from the Downloads section on this page:http://www.parallax.com/product/28125
There, I hope I did not overwhelm you with links!
A more advanced microcontroller is the Propeller which contains 8 cogs and can be programmed is number of languages including C. There is also a Propeller BOE (Board of Education) which is not the same as the BOE that uses the BASIC Stamp 2 (BS2).
A good starting kit is the BASIC Stamp Activity Kit which includes What's a Microcontroller and the BASIC Stamp Homework Board.
Give Parallax a call and talk to the education department. They should be able to set you up and give you access to the Education forum on here. Only educators have access to that forum but you can always ask questions on the other forums.
Someone told me, in response to an earlier post of mine, that I had 8 cogs to work with. Probably due to a miscommunication on my part. I was confused. Thanks for clearing that up. Also, thank you for telling me more about the FREQOUT (optional multiple notes). I should have read the language reference! :P
I hope you have fun with the kids! Giving junior high students very simple pre-written programs and then letting them experiment with changing arguments (such as duration, freq1, and freq2) can be very absorbing as they experience that direct cause and effect. Music is a good choice. I've seen kids who were apathetic about blinking lights or nervous about wiring up push-buttons get excited when the sound effects start, especially if they have had music lessons elsewhere.
Let us know how it goes!
You should look at what Jessica Uelmen posted.
Christmas is coming!
http://forums.parallax.com/showthread.php/118098-Christmas-Caroling-Device-with-Light-Display?highlight=caroling+device+lights
Well! It went pretty poorly. But I did learn something very important. It is VERY important to include discrete goals (mini labs, projects, milestones, whatever you want to call them) in the lesson! The direction I gave them was much, much too loose. Thanks again for your help, Steph!