Shop OBEX P1 Docs P2 Docs Learn Events
Help with a sound effect device — Parallax Forums

Help with a sound effect device

QuornesQuornes Posts: 4
edited 2010-05-10 15:44 in BASIC Stamp
I'm a senior in highschool, and am working on my senior project, a sound effect device. My idea was to have it work by pushing a two button combination (out of four buttons) which would then play back a sound. I haven't done any real BASIC programming before, although I was learning Ruby when I started working on this. When it came time for me to test out my programming, it predictably didn't work. I was wondering if anyone had any advice for me on it, or suggestions. I know my code structure is probably very bad, and I might be using some of the wrong commands
.

Theres a link to my device schematic here

http://yfrog.com/4vseniorprojectschematicp

Im using the Board of Education development board, and the SOMO-14D Audio Sound Module.

This is the link to the sound module page, and its data sheet (which is in a pdf format)
www.4dsystems.com.au/prod.php?id=73
www.4dsystems.com.au/downloads/Audio-Sound-Modules/SOMO-14D/Docs/SOMO-14D-DS-rev3.pdf



And here is the code I wrote



         '{$STAMP BS2}                            'Stamp directive
         '{$PBASIC 2.5}                           'PBASIC directive


         DEBUG "Program Running"


         '----------{I/O}--------------------------------------------

          Data1             PIN         1
          Clock             PIN         2
          Reset             PIN         3
          Button_1          PIN         10
          Button_2          PIN         11
          Button_3          PIN         12
          Button_4          PIN         13

         '----------{SOHO 14D Audio Module Commands}-----------------

         'Play/Pause                     FFFEh
         'Stop                           FFFFh
         'Volume                         FFF0h-FF7h  (In terms of volume)
         'Songs                          0000h,0001h,0002h,0003h,0004h

         'PIN       1                    NEXT
         'PIN       2                    NC
         'PIN       3                    CLK
         'PIN       4                    DATA
         'PIN       5                    BUSY
         'PIN       6                    PLAY/STOP
         'PIN       7                    PREVIOUS
         'PIN       8                    VCC
         'PIN       9                    GND
         'PIN       10                   RESET
         'PIN       11                   SPK+
         'PIN       12                   SPK-
         'PIN       13                   NC
         'PIN       14                   AUDIO

         '----------{Variables}--------------------------------------
          volumechange      VAR          Byte
          songselect        VAR          Byte
          songplay          VAR          Byte
          btnwork           VAR          Byte
          Songfile          VAR          Word
          Play              VAR          Word
          Volume            VAR          Word
          Song              VAR          Word
          VolumeCheck       VAR          Word
         '----------{Constants}--------------------------------------
          BAUD              CON          9600
          SongDelay         CON          10000
         '----------{Initialization}---------------------------------
         DEBUG "Initialization"
         Songfile    = 0

         Play        = 65534

         Volume      = 4087

         Song        = 0

         VolumeCheck = 0

         SHIFTOUT Data1, Clock, MSBPOST ,[noparse][[/noparse]volumechange]
         SHIFTIN  Data1, Clock, MSBPOST ,[noparse][[/noparse]Volume]

         Restart:
              DEBUG "Restart"
              Songfile = 0
              Song     = 0

              LOW Reset

              GOTO Main

         '----------{Program Code}-----------------------------------

         Main:
              DEBUG "Main"
              GOSUB    SubFileSelect
              GOSUB    SubFileSelect2
              GOSUB    SubSongSelect

              SHIFTOUT Data1, Clock, MSBPOST ,[noparse][[/noparse]songplay]
              SHIFTIN  Data1, Clock, MSBPOST ,[noparse][[/noparse]Play]

              PAUSE    SongDelay

              GOTO     Restart

        '----------{Sub Routines}-----------------------------------

          SubFileSelect:
              DEBUG "SubFileSelect"
              BUTTON Button_1, 1, 255, 20, btnwork, 1, Main
                  Songfile = Songfile + 1
              BUTTON Button_2, 1, 255, 20, btnwork, 1, Main
                  Songfile = Songfile + 2
              BUTTON Button_3, 1, 255, 20, btnwork, 1, Main
                  Songfile = Songfile + 4
              BUTTON Button_4, 1, 255, 20, btnwork, 1, Main
                  Songfile = Songfile + 8
              RETURN

          SubFileSelect2:
              DEBUG "SubFileSelect2"
              BUTTON Button_1, 1, 255, 20, btnwork, 1, Main
                  Songfile = Songfile + 10
              BUTTON Button_2, 1, 255, 20, btnwork, 1, Main
                  Songfile = Songfile + 20
              BUTTON Button_3, 1, 255, 20, btnwork, 1, Main
                  Songfile = Songfile + 40
              BUTTON Button_4, 1, 255, 20, btnwork, 1, Main
                  Songfile = Songfile + 80
              RETURN

          SubSongSelect:
             DEBUG "SubSongSelect"
             SELECT Songfile

              CASE   11
                          Song = 0
                          SHIFTOUT Data1, Clock, MSBPOST ,[noparse][[/noparse]songselect]
                          SHIFTIN  Data1, Clock, MSBPOST ,[noparse][[/noparse]Song]
              CASE   12
                          Song = 1
                          SHIFTOUT Data1, Clock, MSBPOST ,[noparse][[/noparse]songselect]
                          SHIFTIN  Data1, Clock, MSBPOST ,[noparse][[/noparse]Song]
              CASE   14
                          Song = 2
                          SHIFTOUT Data1, Clock, MSBPOST ,[noparse][[/noparse]songselect]
                          SHIFTIN  Data1, Clock, MSBPOST ,[noparse][[/noparse]Song]
              CASE   18
                          Song = 3
                          SHIFTOUT Data1, Clock, MSBPOST ,[noparse][[/noparse]songselect]
                          SHIFTIN  Data1, Clock, MSBPOST ,[noparse][[/noparse]Song]
              CASE   21
                          Song = 4
                          SHIFTOUT Data1, Clock, MSBPOST ,[noparse][[/noparse]songselect]
                          SHIFTIN  Data1, Clock, MSBPOST ,[noparse][[/noparse]Song]
              CASE   22
                          Song = 5
                          SHIFTOUT Data1, Clock, MSBPOST ,[noparse][[/noparse]songselect]
                          SHIFTIN  Data1, Clock, MSBPOST ,[noparse][[/noparse]Song]
              CASE   24
                          Song = 6
                          SHIFTOUT Data1, Clock, MSBPOST ,[noparse][[/noparse]songselect]
                          SHIFTIN  Data1, Clock, MSBPOST ,[noparse][[/noparse]Song]
              CASE   28
                          Song = 7
                          SHIFTOUT Data1, Clock, MSBPOST ,[noparse][[/noparse]songselect]
                          SHIFTIN  Data1, Clock, MSBPOST ,[noparse][[/noparse]Song]
              CASE   41
                          Song = 8
                          SHIFTOUT Data1, Clock, MSBPOST ,[noparse][[/noparse]songselect]
                          SHIFTIN  Data1, Clock, MSBPOST ,[noparse][[/noparse]Song]
              CASE   42
                          Song = 9
                          SHIFTOUT Data1, Clock, MSBPOST ,[noparse][[/noparse]songselect]
                          SHIFTIN  Data1, Clock, MSBPOST ,[noparse][[/noparse]Song]
              CASE   44
                          Song = 16
                          SHIFTOUT Data1, Clock, MSBPOST ,[noparse][[/noparse]songselect]
                          SHIFTIN  Data1, Clock, MSBPOST ,[noparse][[/noparse]Song]
              CASE   48
                          Song = 17
                          SHIFTOUT Data1, Clock, MSBPOST ,[noparse][[/noparse]songselect]
                          SHIFTIN  Data1, Clock, MSBPOST ,[noparse][[/noparse]Song]
              CASE   81
                          Song = 18
                          SHIFTOUT Data1, Clock, MSBPOST ,[noparse][[/noparse]songselect]
                          SHIFTIN  Data1, Clock, MSBPOST ,[noparse][[/noparse]Song]
              CASE   82
                          Song = 19
                          SHIFTOUT Data1, Clock, MSBPOST ,[noparse][[/noparse]songselect]
                          SHIFTIN  Data1, Clock, MSBPOST ,[noparse][[/noparse]Song]
              CASE   84
                          Song = 20
                          SHIFTOUT Data1, Clock, MSBPOST ,[noparse][[/noparse]songselect]
                          SHIFTIN  Data1, Clock, MSBPOST ,[noparse][[/noparse]Song]
              CASE   88
                          Song = 21
                          SHIFTOUT Data1, Clock, MSBPOST ,[noparse][[/noparse]songselect]
                          SHIFTIN  Data1, Clock, MSBPOST ,[noparse][[/noparse]Song]
              ENDSELECT

              RETURN




Edited to include info on development board and sound module.
Second edit to include updated code

Post Edited (Quornes) : 5/10/2010 3:28:44 PM GMT

Comments

  • FranklinFranklin Posts: 4,747
    edited 2010-05-05 17:45
    First off just scanning your code,
        IF Songfile = 1 + 1/2  THEN
                              Song = 0
                              SHIFTOUT Data1, Clock, MSBPOST ,[noparse][[/noparse]songselect]
                              SHIFTIN  Data1, Clock, MSBPOST ,[noparse][[/noparse]Song]
                              ENDIF
    
    
    

    This part is almost exactly repeated many times and would be a good candidate for a subroutine. You could do a case to get the song number and then call the sub to play it, much less code and chance for errors.

    Also a link to your sound chip would be helpful.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • QuornesQuornes Posts: 4
    edited 2010-05-05 21:59
    Alright edited first post to include that info. Sorry i didnt put it up first, was thinking that i should of done that during class today.

    With the case command, would something like this work?

    SELECT Songfile
    
                  CASE   1 + 1/2
                      Song = 0
                              SHIFTOUT Data1, Clock, MSBPOST ,[noparse][[/noparse]songselect]
                              SHIFTIN  Data1, Clock, MSBPOST ,[noparse][[/noparse]Song]
                  CASE   1 + 1/3
                      Song = 1
                              SHIFTOUT Data1, Clock, MSBPOST ,[noparse][[/noparse]songselect]
                              SHIFTIN  Data1, Clock, MSBPOST ,[noparse][[/noparse]Song]
    
  • FranklinFranklin Posts: 4,747
    edited 2010-05-06 03:31
    Well in the basic stamp math 1+1/2 =1 and 1+1/3 =0 is that what you are expecting? Also I ment for you to put the shiftouts in a subroutine since they are the same except for the song number which you can set before calling the subroutine.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • QuornesQuornes Posts: 4
    edited 2010-05-06 16:22
    Alright I believe I see what you're saying. With the 1+1/2, the only way i could think of to set up the two button combination was to do something like that. Given that the +1/2 doesnt work, I've changed it so that in the first sequence the 4 buttons add 1, 2, 4, and 8. While in the second part they add 10, 20, 40, 80, which should work out just fine. With the subroutine, is this what you meant?

    SELECT Songfile
    
         CASE   11
            Song = 0
    
         CASE   12
            Song = 1
    
         CASE   14
            Song = 2
    
         CASE   18
            Song = 3
    
    ENDSELECT
    
    GOTO SongShiftIn
    
    
    SongShiftIn:
    
    SHIFTOUT Data1, Clock, MSBPOST ,[noparse][[/noparse]songselect]
    SHIFTIN  Data1, Clock, MSBPOST ,[noparse][[/noparse]Song]
    
    
    
    
  • FranklinFranklin Posts: 4,747
    edited 2010-05-07 02:04
    Not GOTO but GOSUB. Check out the manual for help on syntax and programming style. Also download some of the programs from the Parallax site and look at how the code is written.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • QuornesQuornes Posts: 4
    edited 2010-05-10 15:44
    Thanks for your help Franklin, its helped me improve my code structure and programming style immensely. I've edited the original post to include my current code and changed the attachment to that one also. My current problem though is what to do about the BUTTON command.

    What I currently have is something like this.
    BUTTON Button_1, 1, 255, 20, btnwork, 1, Main
                      Songfile = Songfile + 1
    BUTTON Button_2, 1, 255, 20, btnwork, 1, Main
                      Songfile = Songfile + 2
    BUTTON Button_3, 1, 255, 20, btnwork, 1, Main
                      Songfile = Songfile + 4
    BUTTON Button_4, 1, 255, 20, btnwork, 1, Main
                      Songfile = Songfile + 8
    
    



    BUTTON command works like this

    BUTTON Pin, DownState, Delay, Rate, Workspace, TargetState, Address

    My problem is the Address part. Originally I thought setting it up the way I have it would change Songfile when the button was pushed, but after checking
    "Back to the Basics (PBASIC) With Fun and Games" www.parallax.com/dl/docs/cols/nv/vol1/col/nv35.pdf
    it seems that is not the case. Instead I would have to do something like this.

    BUTTON Button_1, 1, 255, 20, btnwork, 1, Button_1_pushed
    
    Button_1_pushed:
       Songfile = Songfile + 1
    
    



    My question is there a way around doing the above code for each button pushed?
Sign In or Register to comment.