Shop OBEX P1 Docs P2 Docs Learn Events
Need music help for an old newbee — Parallax Forums

Need music help for an old newbee

IroneIrone Posts: 116
edited 2011-07-22 19:41 in BASIC Stamp
I copied a very simple program from "Whats a Microcontroller" to see how you put a pause in a song. When it did not work I replaced the battery with one I just bought a week or so ago. I then put in a debug for the noteLetter and the noteFreq. The breadboard has a piezo speaker hooked up to PIN 8 and ground. Even though the "P" has four zero's after it it still makes a sound. Here's my code. Will somebody tell me how to make a code box? This one did not work out too well!
' {$STAMP BS2}
' {$PBASIC 2.5}
' Bet with pause

Notes      DATA        "G","G","G","e","P","F","F","F","D","Q"

Durations  DATA         8, 8, 8, 2, 8, 8, 8, 8, 2

WholeNote  CON          2000

index VAR Byte

offset VAR Nib

noteLetter VAR Byte

noteFreq VAR Word

noteDuration VAR Word

DO UNTIL noteLetter = "Q"

  READ Notes + index, noteLetter
  LOOKDOWN noteLetter, [ "A", "b", "B", "C", "d",
                       "D", "e", "E", "F", "g",
                       "G", "a", "P", "Q" ], offset

  DEBUG "    ", noteLetter


  LOOKUP offset,       [ 1760, 1865, 1976, 2093, 2217,
                         2349, 2489, 2637, 2794, 2960,
                         3136, 3322, 0, 0 ], noteFreq

  DEBUG "      ", DEC4 noteFreq


  READ Durations + index, noteDuration

noteDuration = WholeNote / noteDuration

  FREQOUT 8, noteDuration, noteFreq

index = index + 1

LOOP

END

Thanks Casey

Comments

  • Nick ErnstNick Ernst Posts: 138
    edited 2011-07-19 11:12
    Casey,
    Unfortunately in this instance the "P" is still a note. You can change the duration of the "P" since it is considered a note and increase the pause to the duration of a normally played note. However, since all note durations are fractions of a whole note value, and you cannot individually specify whole note values for each note, you are limited on the length of your pause. You can add P's into your code, but I found that you still get the chirp from the speaker that you mentioned. Hopefully this helps!
  • IroneIrone Posts: 116
    edited 2011-07-20 00:43
    One question, if the "P" has a value of 0 (no frequency) and the "Q" has a value of 0 (no frequency) why are there only 9 sounds for 10 spaces? Why does it not make a sound at the "Q"?
  • IroneIrone Posts: 116
    edited 2011-07-22 19:41
    I might not know much but I am persistant. I did some digging and found something that works. Look at this:
    '{$STAMP BS2}
    '{$PBASIC 2.0}
    
    
    '************************************************************************
    '*  Basic Stamp Activity Board Sample Program             Player Piano  *
    '*  9 September, 1997                                           (BS-2)  *
    '*                                                                      *
    '*  This program demonstrates the use of the speaker and a couple of    *
    '*  look-up tables. Basically, the program emulates a one note player   *
    '*  piano. Hit the P5/P10 button to play the selected song. Hit the     *
    '*  P6/P9 button to select the next song. Hitting the P7/P8 button      *
    '*  selects the previous song. Hit 'Alt-R' and prepare to be amused.    *
    '*  Feel free to add songs as there is plenty of room on the BS2.       *
    '************************************************************************
    
    C        CON        65                'Ridiculously low notes
    Db        CON        69
    D        CON        73
    Eb        CON        77
    E        CON        82
    F        CON        87
    Gb        CON        92
    G        CON        97
    Ab        CON        103
    A        CON        110
    Bb        CON        117
    BE        CON        124
    
    C1        CON        131                'Very low notes
    Db1        CON        139
    D1        CON        147
    Eb1        CON        154
    E1        CON        165
    F1        CON        175
    Gb1        CON        185
    G1        CON        195
    Ab1        CON        207
    A1        CON        220
    Bb1        CON        234
    BE1        CON        248
    
    C2        CON        262                'Low notes
    Db2        CON        278
    D2        CON        294
    Eb2        CON        307
    E2        CON        329
    F2        CON        350
    Gb2        CON        370
    G2        CON        391
    Ab2        CON        414
    A2        CON        439
    Bb2        CON        467
    BE2        CON        495
    
    C3        CON        521                'Middle 'C'
    Db3        CON        554
    D3        CON        588
    Eb3        CON        623
    E3        CON        658
    F3        CON        694
    Gb3        CON        737
    G3        CON        781
    Ab3        CON        829
    A3        CON        877
    Bb3        CON        928
    BE3        CON        980
    
    C4        CON        1042                'High notes
    Db4        CON        1102
    D4        CON        1163
    Eb4        CON        1240
    E4        CON        1316
    F4        CON        1389
    Gb4        CON        1476
    G4        CON        1563
    Ab4        CON        1658
    A4        CON        1754
    Bb4        CON        1856
    BE4        CON        1960
    
    C5        CON        2084                'Very high notes
    Db5        CON        2204
    D5        CON        2326
    Eb5        CON        2480
    E5        CON        2632
    F5        CON        2778
    Gb5        CON        2952
    G5        CON        3126
    Ab5        CON        3316
    A5        CON        3508
    Bb5        CON        3712
    BE5        CON        3920
    
    C6        CON        4168                'Rediculously high notes
    Db6        CON        4408
    D6        CON        4652
    Eb6        CON        4960
    E6        CON        5264
    F6        CON        5556
    Gb6        CON        5904
    G6        CON        6252
    Ab6        CON        6632
    A6        CON        7016
    Bb6        CON        7424
    BE6        CON        7840
    
    MaxNum        CON        3
    R        CON        $FF
    indx        VAR        Byte
    tune        VAR        Byte
    note        VAR        Word
    dura        VAR        Word
    
    INIT
            tune = 4
    
    
    
    SHOW4:        DEBUG "Bet with better pause",CR
    
    LOOP                                        'Get the next note to be played
    
    Song4
            LOOKUP indx,[G5, G5, G5, EB5, R, F5, F5, F5, db5, 0],note
            LOOKUP indx,[250, 250, 250, 500, 250, 250, 250, 250, 500],dura
            IF note = 0 THEN DELAY                'it.
            IF note = R THEN REST
            FREQOUT 8,dura,note
    NXT
           indx = indx + 1
            GOTO LOOP
    REST
            PAUSE dura
            GOTO NXT
    
    DELAY
            note = 0
            dura = 0
            indx = 0
            PAUSE 200
        STOP
    
    I only changed what was needed but, with a little work I think I can simplify it some.
Sign In or Register to comment.