'{$STAMP BS2}
'{$PBASIC 2.5}
' **************************************************************
' * Rogue Robotics uMP3/Basic Stamp II Trigger System *
' * By Vern Graner SSE, Texas Information Services *
' * http://www.spiderspreyground.com/howto vern [at] txis.com *
' **************************************************************
' * Code demonstrates the uMP3 being used for triggers *
' * Pin Configuration: *
' * Pin 2- N.O. pushbutton to GND (pulled high) *
' * Pin 3- N.O. pushbutton to GND (pulled high) *
' * Pin 4- uMP3 T *
' * Pin 5- uMP3 R *
' **************************************************************
' *Revision Info: V2.0c (VLG) 8-26-2005 *
' **************************************************************
' ************************
' * I/O Definitions *
' *************************************************************************
BUT2 PIN 2 ' N.O. Momentary Push Button to GND on PIN2
BUT3 PIN 3 ' N.O. Momentary Push Button to GND on PIN3
MP3T PIN 4 ' uMP3: set the pin where the uMP3 "T" pin is connected
MP3R PIN 5 ' uMP3: set the pin where the uMP3 "R" pin is connected
' ************************
' * Constant definitions *
' *************************************************************************
N9600T CON 84 ' uMP3: Set the 9600 8 bit no parity TRUE
' ************************
' * Variables *
' *************************************************************************
serdata VAR Byte ' Set aside for input from the uMP3
Volume VAR Byte ' Volume control variable 0-255, (255=MUTE)
TriggerValue VAR Byte ' sensors results stored here
' ************************
' * Main Program Begin *
' *************************************************************************
' *************************
' * Initialize uMP3 unit *
' *************************
SEROUT MP3R,N9600T,[CR] 'Send an INIT CR to the uMP3
GOSUB uReadMP3 'Check for response from uMP3
GOSUB uVolReset 'Reset the uMP3 volume to FULL
GOSUB uStop 'Tell the unit to STOP in case it's playing
' *************************
' * Play Ambient Sound *
' *************************
Ambient:
SEROUT MP3R,N9600T,["PC F /Suspense-Queue-0.mp3",CR]
DEBUG " ",CR
DEBUG "Play Ambient Sound Loop",CR
' *************************
' * Wait for triggers *
' *************************
MainLoop:
DO
TriggerValue=INA&%1100 ' check all triggers at once
SEROUT MP3R,N9600T,["PC Z",CR] ' \ Send status check request To uMP3
GOSUB uReadMP3 ' | Read response from uMP3
IF serdata = "S" THEN GOTO AMBIENT ' | If the player has finished playing the file, start the ambience over
PAUSE 100 ' / Wait a bit before we check to see if the song is done.
LOOP UNTIL Triggervalue <>12 ' If no triggers continue to loop
IF (8=TriggerValue) THEN ' Check the values to determine which trigger and
DEBUG "Trigger 1 Detectd",CR ' branch to the appropriate sample
GOTO Sound1
ELSEIF (4=TriggerValue) THEN
DEBUG "Trigger 2 Detectd",CR
GOTO Sound2
ELSEIF (0=TriggerValue) THEN
DEBUG "Both Triggers Detected",CR
GOTO Sound3
ELSE
DEBUG "Error Trapped!",CR
ENDIF
GOTO MainLoop
' ************************
' * Sounds *
' *************************************************************************
Sound1:
SEROUT MP3R,N9600T,["PC F /Dramatic-Cue-EndingHit-0.mp3",CR]
DEBUG "Playing: Dramatic-Cue-EndingHit-0.mp3",CR
' GOSUB WaitRelease 'If you want the ambient to return when trigger released
GOSUB uWaitDone 'If you want the sound to play all the way b4 returning
GOTO Ambient
Sound2:
SEROUT MP3R,N9600T,["PC F /Dramatic-Reveal-Horns1.mp3",CR]
DEBUG "Playing: Dramatic-Reveal-Horns1.mp3",CR
GOSUB WaitRelease 'If you want the ambient to return when trigger released
' GOSUB uWaitDone 'If you want the sound to play all the way b4 returning
GOTO Ambient
Sound3:
SEROUT MP3R,N9600T,["PC F /Dramatic-Cue-Fast-0.mp3",CR]
DEBUG "Playing: Dramatic-Cue-Fast-0.mp3",CR
' GOSUB WaitRelease 'If you want the ambient to return when trigger released
GOSUB uWaitDone 'If you want the sound to play all the way b4 returning
GOTO Ambient
' ************************
' * Subroutines *
' *************************************************************************
uStop: 'Stop the player
DEBUG "Send STOP command to player:",CR
DEBUG "PC S",CR
SEROUT MP3R,N9600T,["PC S",CR]
GOSUB uReadMP3
PAUSE 500
RETURN
uVolReset: 'Reset the volume to "full"
DEBUG "Send VOLUME command to player:",CR
DEBUG "ST V",DEC VOLUME," ",CR
VOLUME=0 'Volume 255=mute 0=full
SEROUT MP3R,N9600T,["ST V ",DEC Volume,CR]
GOSUB uReadMP3
PAUSE 500
RETURN
uWaitDone: ' Wait here till the current song finishes
DEBUG "Waiting for Song End...",CR
WAITING:
SEROUT MP3R,N9600T,["PC Z",CR] ' Check the status of the uMP3
SERIN MP3T, N9600T, 1000, noData1, [serdata] ' Get the response
' DEBUG serdata,CR ' Debug to see what the uMP3 is returning
IF serdata = "S" THEN RETURN ' If the player has finished playing the file, jump to the start
noData1:
PAUSE 100 ' otherwise, kill some time
GOTO WAITING
uReadMP3: 'Read command response from uMP3 unit
SERIN MP3T, N9600T, 1000, No_Resp1, [serdata]
RETURN
No_Resp1: 'Tell user there is no reply from uMP3
' DEBUG "No response from uMP3!",CR
RETURN
WaitRelease: 'Wait for the trigger to be released b4 returning to ambient
DEBUG "Waiting for release...",CR
PAUSE 250
WaitRelease2:
IF BUT3 = 0 THEN WaitRelease2
DEBUG "Released!",CR
RETURN