Shop OBEX P1 Docs P2 Docs Learn Events
teaching project: sound/music with PBASIC/BOE Bot/piezospeaker — Parallax Forums

teaching project: sound/music with PBASIC/BOE Bot/piezospeaker

anticipateanticipate Posts: 10
edited 2013-11-19 07:13 in General Discussion
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:
' {$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

  • Steph LindsaySteph Lindsay Posts: 767
    edited 2013-11-15 14:08
    Hi anticipate, and welcome to the forums! I'll add my answers below your questions.

    ********************************


    #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!
  • GenetixGenetix Posts: 1,754
    edited 2013-11-15 14:09
    What's a Microcontroller (WAM) is the starter kit for anyone with little electronics or programming experience. Chapter 8 is all about sound and music using the piezospeaker from simple tones to songs. Generally you start with WAM before moving on to the other kits such as the BOE-Bot Robot Kit since it requires knowledge of programming and hooking up electronics to the microcontroller. Both of these kits use the BASIC Stamp 2 which uses PBASIC.

    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.
  • anticipateanticipate Posts: 10
    edited 2013-11-15 15:57
    Update: Here's what I ended up with in terms of the basic song. No chords or anything, sadly, but I have a hard time reading sheet music, so it's the best I could hope for anyway, I think. If anyone can help me get the number of milliseconds correct, based on the tempo, that would be helpful. It's kind-of working as is. I may go through and add in 'chords', but then again I may not . . . This ought to do for now.
    -----------------------
    '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 ]-----------
    
    Tempo CON 152                 'CHANGEABLE
    
    Piezo   PIN   4               ' Speaker
    
    C4  CON    262
    Db4 CON   277
    D4  CON   294
    Eb4 CON   311
    E4  CON   330
    F4  CON   349
    Gb4 CON   370
    G4  CON   392
    Ab4 CON   415
    A4  CON   440
    Bb4 CON   466
    Be4 CON   494                 'because 'B4' is apparently predefined
    
    C5  CON   523
    D5  CON   587
    E5  CON   659
    F5  CON   698
    G5  CON   784
    A5  CON   880
    Be5 CON   988
    
    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
    Cs7 CON    2217
    D7  CON    2349
    Ds7 CON    2489
    E7  CON    2637
    F7  CON    2793
    Fs7 CON    2960
    G7  CON    3136
    Gs7 CON    3322
    A7  CON    3520
    Be7 CON    3951
    
    Wn VAR  Word 'full note
    Wno VAR  Word 'full note with dot
    Hn VAR  Word
    Hno VAR  Word
    Qn VAR  Word
    Qno VAR  Word
    En VAR  Word
    Eno VAR  Word
    Sn VAR  Word
    Sno VAR  Word
    
    Wn = 1000/(Tempo/15) 'based on Tempo variable
    Wno = Wn*(15/10)     'o indicates dotted note
    Hn = Wn/2            'half note
    Hno = Hn*(15/10)
    Qn = Hn/2            'quarter note
    Qno = Qn*(15/10)
    En = Qn/2            'eighh note
    Eno = En*(15/10)
    Sn = En/2            'sixteenth note
    Sno = Sn*(15/10)
    
    'Measure
    
    FREQOUT Piezo, Wn, A7
    FREQOUT Piezo, Qn, E7
    FREQOUT Piezo, Qn, E7
    FREQOUT Piezo, Qn, A7
    
    FREQOUT Piezo, En, G7
    FREQOUT Piezo, En, F7
    FREQOUT Piezo, Wn, G7
    PAUSE Hn
    
    FREQOUT Piezo, Wn, A7
    FREQOUT Piezo, Qn, F7
    FREQOUT Piezo, Qn, F7
    FREQOUT Piezo, Qn, A7
    
    FREQOUT Piezo, En, Gs7
    FREQOUT Piezo, En, Fs7
    FREQOUT Piezo, Wn, Gs7
    PAUSE Hn
    
    FREQOUT Piezo, Qn, A7
    FREQOUT Piezo, Qn, E7
    FREQOUT Piezo, En, E7
    FREQOUT Piezo, En, A7
    FREQOUT Piezo, Sn, A7
    FREQOUT Piezo, Sn, Be7
    FREQOUT Piezo, Sn, Cs7
    FREQOUT Piezo, Sn, D7
    
    FREQOUT Piezo, Wn, E7
    FREQOUT Piezo, En, E7
    FREQOUT Piezo, En, E7
    FREQOUT Piezo, Sn, E7
    FREQOUT Piezo, Sn, F7
    FREQOUT Piezo, Sn, G7
    
    FREQOUT Piezo, Wn, A7
    FREQOUT Piezo, En, A7
    FREQOUT Piezo, En, A7
    FREQOUT Piezo, Sn, A7
    FREQOUT Piezo, Sn, G7
    FREQOUT Piezo, Sn, F7
    
    FREQOUT Piezo, En, G7
    FREQOUT Piezo, Sn, F7
    FREQOUT Piezo, Hn, E7
    FREQOUT Piezo, Qn, E7
    
    FREQOUT Piezo, En, D7
    FREQOUT Piezo, Sn, D7
    FREQOUT Piezo, Sn, E7
    FREQOUT Piezo, Hn, F7
    FREQOUT Piezo, En, E7
    FREQOUT Piezo, En, D7
    
    FREQOUT Piezo, En, C7
    FREQOUT Piezo, Sn, C7
    FREQOUT Piezo, Sn, D7
    FREQOUT Piezo, Wn, E7
    FREQOUT Piezo, En, D7
    FREQOUT Piezo, En, C7
    
    FREQOUT Piezo, En, Be6
    FREQOUT Piezo, Sn, Be6
    FREQOUT Piezo, Sn, Cs7
    FREQOUT Piezo, Wn, Ds7
    FREQOUT Piezo, Qn, Fs7
    
    FREQOUT Piezo, En, E7
    FREQOUT Piezo, Sn, E6
    FREQOUT Piezo, Sn, E6
    FREQOUT Piezo, En, E6
    FREQOUT Piezo, Sn, E6
    FREQOUT Piezo, Sn, E6
    FREQOUT Piezo, En, E6
    FREQOUT Piezo, Sn, E6
    FREQOUT Piezo, Sn, E6
    FREQOUT Piezo, En, E6
    FREQOUT Piezo, En, E6
    
    FREQOUT Piezo, Qn, A6
    FREQOUT Piezo, En, E6
    FREQOUT Piezo, En, E6
    FREQOUT Piezo, En, A6
    FREQOUT Piezo, Sn, A6
    FREQOUT Piezo, Sn, Be6
    FREQOUT Piezo, Sn, Cs7
    FREQOUT Piezo, Sn, D7
    
    FREQOUT Piezo, Wn, E7
    FREQOUT Piezo, En, E7
    FREQOUT Piezo, En, E7
    FREQOUT Piezo, En, E7
    FREQOUT Piezo, En, F7
    FREQOUT Piezo, En, G7
    
    FREQOUT Piezo, Wn, A7
    FREQOUT Piezo, Qn, C7
    
    FREQOUT Piezo, Qn, Be7
    FREQOUT Piezo, Wn, Gs7
    FREQOUT Piezo, Qn, E7
    
    FREQOUT Piezo, Wno, F7
    FREQOUT Piezo, Qn, A7
    
    FREQOUT Piezo, Qn, Gs7
    FREQOUT Piezo, Wn, E7
    FREQOUT Piezo, Qn, E7
    
    FREQOUT Piezo, Wno, F7
    FREQOUT Piezo, Qn, A7
    
    FREQOUT Piezo, Qn, Gs7
    FREQOUT Piezo, Wn, E7
    FREQOUT Piezo, Qn, Cs7
    
    FREQOUT Piezo, Wno, D7
    FREQOUT Piezo, Qn, F7
    
    FREQOUT Piezo, Qn, E7
    FREQOUT Piezo, Wn, C7
    FREQOUT Piezo, Qn, A6
    
    FREQOUT Piezo, En, Be6
    FREQOUT Piezo, Sn, Be6
    FREQOUT Piezo, Sn, Cs7
    FREQOUT Piezo, Wn, Ds7
    FREQOUT Piezo, Qn, Fs7
    
    FREQOUT Piezo, En, E7
    FREQOUT Piezo, Sn, E6
    FREQOUT Piezo, Sn, E6
    FREQOUT Piezo, En, E6
    FREQOUT Piezo, Sn, E6
    FREQOUT Piezo, Sn, E6
    FREQOUT Piezo, Wn, E6
    
    FREQOUT Piezo, Wno, D7
    FREQOUT Piezo, Qn, F7
    
    FREQOUT Piezo, Qn, E7
    FREQOUT Piezo, Wn, C7
    FREQOUT Piezo, Qn, A6
    
    FREQOUT Piezo, En, Be6
    FREQOUT Piezo, Sn, Be6
    FREQOUT Piezo, Sn, Cs7
    FREQOUT Piezo, Wn, Ds7
    FREQOUT Piezo, Qn, Fs7
    
    FREQOUT Piezo, En, E7
    FREQOUT Piezo, Sn, E6
    FREQOUT Piezo, Sn, E6
    FREQOUT Piezo, En, E6
    FREQOUT Piezo, Sn, E6
    FREQOUT Piezo, Sn, E6
    FREQOUT Piezo, Wn, E6
    
  • anticipateanticipate Posts: 10
    edited 2013-11-15 16:02
    You didn't overwhelm me with links. You helped me find great resources. Thank you. I hope I didn't overwhelm you with questions. I think you answered them appropriately. Also, I'm sorry for asking these questions without doing my homework. I was given part of the kit and exactly no time to learn or prepare. So thanks again . . .

    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
  • Steph LindsaySteph Lindsay Posts: 767
    edited 2013-11-15 16:32
    You are most welcome. ! I'm glad I could help you ramp up quickly.

    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!
  • IroneIrone Posts: 116
    edited 2013-11-15 17:16
  • anticipateanticipate Posts: 10
    edited 2013-11-18 18:23
    Let us know how it goes!

    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!
  • Steph LindsaySteph Lindsay Posts: 767
    edited 2013-11-19 07:13
    Ah well! C'est la vie. Kudos for your effort, and I'm glad it was educational for you! I hope at least some fun was had, and that you get to try it again some time.
Sign In or Register to comment.