' =========================================================================
'
' File...... Coffin_popup v1.2.BS1
' Purpose... Coffin pop-up routine (Prop-1, RC-4 , AP-8)
' Author....
' E-mail....
' Started...
' Updated...
'
' {$STAMP BS1}
' {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------
' This program will wait FOR contact switch or PIR to close then trigger coffin
' pop-up and 120V light. After small pause, head will turn 45 degrees, stop, and
' a 30 sec clip will play. Head will turn back, pop-up will drop and light will
' go off.
'
' Prop-1 moded ULN pin 1 cut/P7 jumper OFF/P6 jumper ON/DWN
' Prop-1 P7 is connected to RC-4
' Prop-1 P6 is trigger to start connected to WR
' Prop-1 P0 is connected to head servo
' Prop-1 OUT2/V+ is connected to 24V solenoid
' AP-8 power from Prop-1 V+ and GND
' NOTE: ALL DIP SWITCHES ON AP-8 MUST BE OPEN FOR SERIAL COM


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


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

SYMBOL Sio = 7
SYMBOL servo = 0
SYMBOL Trigger = PIN6 ' SETUP = DN
SYMBOL valve = PIN2 ' OUT2 and V+ (24V to valve)

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

SYMBOL IsOn = 1
SYMBOL IsOff = 0

SYMBOL AllOn = %1111
SYMBOL AllOff = %0000

SYMBOL CW = 100
SYMBOL CCW = 180


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

SYMBOL relays = B0 ' RC-4 relays
SYMBOL light = BIT0
SYMBOL fog = BIT1 ' Relays 2-4 not used
'SYMBOL K3 = BIT2
'SYMBOL K4 = BIT3

SYMBOL sfx = B2

SYMBOL sweep = B3 ' span of movement
SYMBOL hold = B4 ' hold time (larger = slower)


' -----[ EEPROM Data ]-----------------------------------------------------


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

Reset:
PINS = %00000000
DIRS = %00000100
LOW servo

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

GOSUB Kill_All

Main:
SEROUT Sio, OT2400, ("!AP8", %00, "P", 4) ' loop background sound
IF Trigger = IsOff THEN Main ' wait for pressure switch


' Pop up/turn on strobe/play sound
PAUSE 25
SEROUT Sio, OT2400, ("!AP8", %00, "X") '
PAUSE 25
SEROUT Sio, OT2400, ("!AP8", %00, "P", 0) ' play scream sound
PAUSE 500 ' increase this value if pop/sound don't match
valve = IsOn ' activate valve
light = IsOn ' activate strobe
fog = IsOn ' activate fog
GOSUB Set_Relays

' Turn head
FOR sweep = CW TO CCW ' swing full span
FOR hold = 1 TO 5 ' slowly
PULSOUT servo, sweep
PAUSE 20
NEXT
NEXT

' Play voice clip
SEROUT Sio, OT2400, ("!AP8", %00, "X")
PAUSE 25
SEROUT Sio, OT2400, ("!AP8", %00, "P", 1) ' play sound 0
PAUSE 19000 ' wait for sound 0 to finish
SEROUT Sio, OT2400, ("!AP8", %00, "P", 4) ' back to background

' Turn head back
FOR sweep = CCW TO CW STEP -1
FOR hold = 1 TO 5
PULSOUT servo, sweep
PAUSE 20
NEXT
NEXT

' Drop/turn off strobe
valve = IsOff
PAUSE 2000
light = IsOff
fog = IsOff
GOSUB Set_Relays

GOTO Main


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

Set_Relays:
SEROUT Sio, OT2400, ("!RC4", %00, "S", relays)
RETURN

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

Play_Sound:
IF sfx > 7 THEN Play_Exit ' bad segment #?
SEROUT Sio, OT2400, ("!AP-8", %00, "P", sfx)

Play_Exit:
RETURN

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

Kill_All:
SEROUT Sio, OT2400, ("!RC4", %00, "X", "!AP-8", %00, "X")
RETURN

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