November 22, 2024, 02:49:53 PM

News:

You can now use Vixen to program your Prop-1 and Prop-2 controllers!  Get started quickly and easily, without having to learn PBASIC.  Details in the Library forum.


Cheapo MP3 Player Controller -- Up to Four Players!

Started by JonnyMac, February 24, 2008, 12:08:53 PM

Previous topic - Next topic

JonnyMac

February 24, 2008, 12:08:53 PM Last Edit: February 24, 2008, 12:11:40 PM by JonnyMac
Per a discussion here vis-a-vis state-machine programming and a thread on HauntForum.com vis-a-vis controlling cheapo MP3 players (with a relay contact across the play button) I have written this program.  It is, essentially, like four delay timers for four MP3 players.  The four trigger inputs are debounced (to prevent false triggering in the electrically-noisy environment of your haunt).  When a valid trigger is detected the relay output is activated and timed out; after that the state (PlayHold) simply ignores new triggers until the MP3 player is done and you want to be able to start it again.

Note that overall timing is in 100 millisecond units, and this time is created by the Scan_Inputs section.

' =========================================================================
'
'   File...... MP3_4x.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK (www.efx-tek.com)
'   E-mail.... jwilliams@efx-tek.com
'              Copyright (c) 2008 EFX-TEK
'              Some Rights Reserved
'              see: http://creativecommons.org/licenses/by/3.0/us/
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

SYMBOL  Trigger4        = PIN7
SYMBOL  Trigger3        = PIN6
SYMBOL  Trigger2        = PIN5
SYMBOL  Trigger1        = PIN4

SYMBOL  Player4         = PIN3
SYMBOL  Player3         = PIN2
SYMBOL  Player2         = PIN1
SYMBOL  Player1         = PIN0


' -----[ Constants ]-------------------------------------------------------

SYMBOL  IsOn            = 1                     ' for active-high in/out
SYMBOL  IsOff           = 0

SYMBOL  Waiting         = 0                     ' player states
SYMBOL  PressBtn        = 1
SYMBOL  PlayHold        = 2


' -----[ Variables ]-------------------------------------------------------

SYMBOL  scan            = B0                    ' trigger scan
SYMBOL   mp3n1          =  BIT4                 '  debounced inputs
SYMBOL   mp3n2          =  BIT5
SYMBOL   mp3n3          =  BIT6
SYMBOL   mp3n4          =  BIT7

SYMBOL  idx             = B1                    ' loop controller

SYMBOL  state1          = B2                    ' state indicators
SYMBOL  state2          = B3
SYMBOL  state3          = B4
SYMBOL  state4          = B5

SYMBOL  timer1          = W3                    ' state timers
SYMBOL  timer2          = W4
SYMBOL  timer3          = W5
SYMBOL  timer4          = W6


' -----[ Initialization ]--------------------------------------------------

Reset:
  PINS = %00000000                              ' clear outputs
  DIRS = %00001111                              ' set P0-P3 to output mode


' -----[ Program Code ]----------------------------------------------------

Main:
  scan = %11110000                              ' assume all pressed

  ' debounce inputs (to prevent false triggers)
  ' create loop timing of 100 milliseconds

Scan_Inputs:
  FOR idx = 1 TO 10
    scan = PINS & %11110000 & scan              ' debounce inputs
    PAUSE 10
  NEXT


' -------------
' MP3 Player #1
' -------------
'
Update_Player_1:
  timer1 = timer1 - 1                           ' update timer (if running)
  BRANCH state1, (P1_Wait, P1_Press, P1_Hold)

P1_Wait:                                        ' wait for trigger
  IF mp3n1 = IsOff THEN Update_Player_2         ' skip if no press
    state1 = PressBtn                           ' update state
    Player1 = IsOn                              ' start player
    timer1 = 1                                  ' hold for 0.2 secs
    GOTO Update_Player_2

P1_Press:                                       ' "pressing" start button
  IF timer1 > 0 THEN Update_Player_2
    state1 = PlayHold                           ' update state
    Player1 = IsOff                             ' release play button
    timer1 = 299                                ' 30 secs (minus press time)
    GOTO Update_Player_2

P1_Hold:
  IF timer1 > 0 THEN Update_Player_2
    state1 = Waiting


' -------------
' MP3 Player #2
' -------------
'
Update_Player_2:
  timer2 = timer2 - 1
  BRANCH state2, (P2_Wait, P2_Press, P2_Hold)

P2_Wait:
  IF mp3n2 = IsOff THEN Update_Player_3
    state2 = PressBtn
    Player2 = IsOn
    timer2 = 1
    GOTO Update_Player_3

P2_Press:
  IF timer2 > 0 THEN Update_Player_3
    state2 = PlayHold
    Player2 = IsOff
    timer2 = 299
    GOTO Update_Player_3

P2_Hold:
  IF timer2 > 0 THEN Update_Player_3
    state2 = Waiting


' -------------
' MP3 Player #3
' -------------
'
Update_Player_3:
  timer3 = timer3 - 1
  BRANCH state3, (P3_Wait, P3_Press, P3_Hold)

P3_Wait:
  IF mp3n3 = IsOff THEN Update_Player_4
    state3 = PressBtn
    Player3 = IsOn
    timer3 = 1
    GOTO Update_Player_4

P3_Press:
  IF timer3 > 0 THEN Update_Player_4
    state3 = PlayHold
    Player3 = IsOff
    timer3 = 299
    GOTO Update_Player_4

P3_Hold:
  IF timer4 > 0 THEN Update_Player_4
    state3 = Waiting


' -------------
' MP3 Player #4
' -------------
'
Update_Player_4:
  timer4 = timer4 - 1
  BRANCH state4, (P4_Wait, P4_Press, P4_Hold)

P4_Wait:
  IF mp3n4 = IsOff THEN Updates_Done
    state4 = PressBtn
    Player4 = IsOn
    timer4 = 1
    GOTO Updates_Done

P4_Press:
  IF timer4 > 0 THEN Updates_Done
    state4 = PlayHold
    Player4 = IsOff
    timer4 = 299
    GOTO Updates_Done

P4_Hold:
  IF timer4 > 0 THEN Updates_Done
    state4 = Waiting


Updates_Done:
  GOTO Main


' -----[ Subroutines ]-----------------------------------------------------


' -------------------------------------------------------------------------


' -----[ User Data ]-------------------------------------------------------
Jon McPhalen
EFX-TEK Hollywood Office