November 22, 2024, 10:18:19 AM

News:

Be sure to checkout our Vixen interfaces in the Library forum -- if you want PC automation at near zero cost, EFX-TEK and Vixen is a great combination of tools.


need help trimming down code

Started by ChrisBartram, October 23, 2013, 10:33:42 PM

Previous topic - Next topic

ChrisBartram

Sorry for the ugly code but am up at midnight trying to hobble together a quick program for a new PROP1 powered prop that I've been messing with - hoping to have it ready for our opening Friday night. It has both a fully loaded RC4 and a uMP3 I need to control, and when I try to compile/run it I'm getting "EEPROM FULL"... too many long serial commands I guess.

Code below with comments that describe the desired sequence. Thanks!
-Chris


' Program:  The Toxic Well
' Author:   Chris Bartram
' Modified: 10/16/2013
' Comments:  This program controls the "Toxic Dump Well" prop
'
' -----[ I/O Definitions ]-------------------------------------------
'
' Connections:
' 7: Pins    PIR
' 6:
' 5:
' 4: Pins    RC4 Relay controller
' 3: Pins    uMP3 transmit controlling sound
' 2: Pins    uMP3 receive
' 1:
' 0:
'
' Serial connections
SYMBOL PIR=PIN7
SYMBOL RC4sio  = 4
SYMBOL VTX=3            ' uMP3 sound player
SYMBOL VRX=2

' Devices on the RC4 controller
SYMBOL RC4Blklight = 1
SYMBOL RC4Fog = 2
SYMBOL RC4Popup = 3 '(plus red &yellow floods)
SYMBOL RC4Fan = 4

' RC4 Settings
SYMBOL RC4baud = OT2400
SYMBOL RC4addr = %11

SYMBOL IsOn = 1
SYMBOL IsOff = 0
SYMBOL COUNTER=W2
SYMBOL char=B2
SYMBOL TriggerDelay=5000 ' delay between motion detected and start
SYMBOL Resettime=25000   ' delay before prop reset

' -----[ Initialization ]--------------------------------------------
HIGH VTX
PINS = %00000000
SEROUT RC4sio, RC4baud, ("!RC4", RC4addr, "X") ' turn off all relays
PAUSE 10000 ' pause 10 seconds (wait for uMP3 to initialize)

GOSUB Check_uMP3

'DEBUG CLS                  ' Clear DEBUG Screen
FOR counter = 20 TO 0 STEP -1     ' Wait another 30 Seconds For PIR Warm-Up
DEBUG "IR Ready in:", counter 'print how much time left
  PAUSE 1000               ' Display Counter Every Second
DEBUG CLS                  ' Clear DEBUG Screen
NEXT

counter = 0                ' Clear Counter Variable

DEBUG "WAITING...   ",CR      ' Display Waiting Message

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

WaitForMotion:
    PAUSE 25
    counter = counter + 50 * PIR
    IF counter < 250 THEN WaitForMotion              ' Motion Detected?

   DEBUG "TRIPPED..."

' motion detected

PropSequence:
' Wait 5 seconds
' Turn on black lights
' Turn on Fog Machine
' Wait 3 seconds
' Turn on Fans
' wait 2 more seconds
' turn off fog
' Trigger uMP3 sound
' Trigger Popup(solenoid) with floods
' Wait 3 seconds
' Turn off Popup
' Wait 5 seconds
' Turn off Fans
' Wait 25 seconds
' Turn off black lights
' Restart sequence

    PAUSE Triggerdelay
'   DEBUG "Black Lights ON",CR
    SEROUT RC4sio, RC4baud, ("!RC4", RC4addr, "R", RC4Blklight, IsOn)
'   DEBUG "Fog ON",CR
    SEROUT RC4sio, RC4baud, ("!RC4", RC4addr, "R", RC4Fog, IsOn)
    PAUSE 3000  ' 3 seconds of fog
'   DEBUG "Fan ON",CR
    SEROUT RC4sio, RC4baud, ("!RC4", RC4addr, "R", RC4Fan, IsOn)
    PAUSE 2000  ' 2 more seconds
'   DEBUG "Fog OFF",CR
    SEROUT RC4sio, RC4baud, ("!RC4", RC4addr, "R", RC4Fog, IsOff)
'   DEBUG "Start SOUND! ",CR
    SEROUT VTX, T2400, ("ST O 1", 13)  ' set to play only once
    SERIN VRX, T2400, char
    SEROUT VTX, T2400, ("PC F /SCREAM.MP3", 13)
    SERIN VRX, T2400, char
'   DEBUG "Popup UP",CR
    SEROUT RC4sio, RC4baud, ("!RC4", RC4addr, "R", RC4Popup, IsOn)
    PAUSE 3000
'   DEBUG "Popup Down",CR
    SEROUT RC4sio, RC4baud, ("!RC4", RC4addr, "R", RC4Popup, IsOff)
    PAUSE 5000
'   DEBUG "Fan OFF",CR
    SEROUT RC4sio, RC4baud, ("!RC4", RC4addr, "R", RC4Fan, IsOff)
    PAUSE Resettime
'   DEBUG "All Relays OFF",CR
    SEROUT RC4sio, RC4baud, ("!RC4", RC4addr, "X") ' turn off all relays

    GOSUB Check_uMP3

    DEBUG "waiting for motion",CR
    counter =0
    GOTO WaitForMotion

Check_uMP3:
' DEBUG "serout"
  SEROUT VTX, T2400, (13)
  PAUSE 100
  SEROUT VTX, T2400, (13)
  SERIN VRX, T2400, char
  DEBUG "in mp3 loop",#@char,CR
  IF char <> ">" THEN Check_uMP3

  SEROUT VTX, T2400, ("ST O 0", 13)  ' set to loop forever
  SERIN VRX, T2400, char
  DEBUG "ST result=",#@char,CR
  SEROUT VTX, T2400, ("PC F /BKGROUND.MP3", 13)
  SERIN VRX, T2400, char
  DEBUG "play result=",#@char,CR
  RETURN

BigRez

Here's a version that reduces the footprint and least let you load it up.  Notice that when setting up the RC4, rather than setting each individual relay, I use the S command to set the status of all relays at once. This allows a single Set_RC4 subroutine to be used and eliminate many redundant SEROUT commands (each taking about 12 bytes.)

You could probably do something similar with the uMP3 code if you need to squeeze more space.

I left much of your code intact but commented out most of the DEBUG statements as they'll take up space as well, and comments out the little loop in the beginning.

My RC4 is tied up right now so I couldn't test this out, but at least it's a start. It's also late after a long day so standard quality disclaimer applies.  :)

' {$STAMP BS1}
                                         ' Program:  The Toxic Well
' Author:   Chris Bartram
' Modified: 10/16/2013
' Comments:  This program controls the "Toxic Dump Well" prop
'
' -----[ I/O Definitions ]-------------------------------------------
'
' Connections:
' 7: Pins    PIR
' 6:
' 5:
' 4: Pins    RC4 Relay controller
' 3: Pins    uMP3 transmit controlling sound
' 2: Pins    uMP3 receive
' 1:
' 0:
'
' Serial connections
SYMBOL PIR=PIN7
SYMBOL RC4sio  = 4
SYMBOL VTX=3            ' uMP3 sound player
SYMBOL VRX=2

SYMBOL IsOn  = 1
SYMBOL IsOff = 0

' RC4 Settings
SYMBOL RC4baud = OT2400
SYMBOL RC4addr = %11

SYMBOL  RC4relays     = B0
SYMBOL   BlkLight      =  BIT0
SYMBOL   Fog           =  BIT1
SYMBOL   Popup         =  BIT2
SYMBOL   Fan           =  BIT3

SYMBOL char           = B2
SYMBOL RC4delay       = B3
SYMBOL COUNTER        = W2

SYMBOL TriggerDelay=5000 ' delay between motion detected and start

InitialRun:
   RC4delay = 10000


' -----[ Initialization ]--------------------------------------------
Reset:
   HIGH VTX
   PINS = %00000000
   DIRS = %00011000                             ' set output pins (1s)

   RC4relays = %0000
   GOSUB Set_RC4

   GOSUB Check_uMP3

'DEBUG CLS                             ' Clear DEBUG Screen
'   FOR counter = 20 TO 0 STEP -1       ' Wait another 30 Seconds For PIR Warm-Up
'      DEBUG "IR Ready in:", counter    'print how much time left
'      PAUSE 1000                       ' Display Counter Every Second
'      DEBUG CLS                        ' Clear DEBUG Screen
'   NEXT

   counter = 0                ' Clear Counter Variable

   DEBUG "WAITING...   ",CR      ' Display Waiting Message

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

WaitForMotion:
    PAUSE 25
    counter = counter + 50 * PIR
    IF counter < 250 THEN WaitForMotion              ' Motion Detected?

'   DEBUG "TRIPPED..."

' motion detected

PropSequence:
' Wait 5 seconds
' Turn on black lights
' Turn on Fog Machine
' Wait 3 seconds
' Turn on Fans
' wait 2 more seconds
' turn off fog
' Trigger uMP3 sound
' Trigger Popup(solenoid) with floods
' Wait 3 seconds
' Turn off Popup
' Wait 5 seconds
' Turn off Fans
' Wait 25 seconds
' Turn off black lights
' Restart sequence


    PAUSE Triggerdelay

'   DEBUG "Blacklight and Fog ON",CR
    BlkLight =  IsOn
    Fog      =  IsOn
    RC4delay = 3000
    GOSUB Set_RC4

'   DEBUG "Fan ON",CR
    Fan = IsOn
    RC4delay = 2000
    GOSUB Set_RC4

'   DEBUG "Fog OFF",CR
    Fog = IsOff
    RC4delay = 0
    GOSUB Set_RC4

'   DEBUG "Start SOUND! ",CR
    SEROUT VTX, T2400, ("ST O 1", 13)  ' set to play only once
    SERIN VRX, T2400, char
    SEROUT VTX, T2400, ("PC F /SCREAM.MP3", 13)
    SERIN VRX, T2400, char

'   DEBUG "Popup UP",CR
    Popup = IsOn
    RC4delay = 3000
    GOSUB Set_RC4

'   DEBUG "Popup Down",CR
    Popup = IsOff
    RC4delay = 5000
    GOSUB Set_RC4

'   DEBUG "Fan OFF",CR
    Fan = IsOn
    RC4delay = 25000
    GOSUB Set_RC4

    GOSUB Check_uMP3

    RC4delay = 0
    GOTO Reset

Check_uMP3:
' DEBUG "serout"
'  SEROUT VTX, T2400, (13)
'  PAUSE 100
  SEROUT VTX, T2400, (13)
  SERIN VRX, T2400, char
'  DEBUG "in mp3 loop",#@char,CR
  IF char <> ">" THEN Check_uMP3

  SEROUT VTX, T2400, ("ST O 0", 13)  ' set to loop forever
  SERIN VRX, T2400, char
'  DEBUG "ST result=",#@char,CR
  SEROUT VTX, T2400, ("PC F /BKGROUND.MP3", 13)
  SERIN VRX, T2400, char
'  DEBUG "play result=",#@char,CR
  RETURN

' =========================================================================
' send the RC-4 serial command using relays value
' -------------------------------------------------------------------------
Set_RC4:
   SEROUT RC4sio, RC4baud, ("!RC4", RC4addr, "S", RC4relays)
   PAUSE RC4delay
   RETURN

JonnyMac

October 24, 2013, 12:29:37 AM #2 Last Edit: October 24, 2013, 12:31:10 AM by JonnyMac
Here's my concise take which does what you want with about 15% memory to spare. You're overworking the code a little bit -- sometimes we need to be slick to fit a complex program into a Prop-1.

Please note that I changed your IO structure. It is my [very strong] opinion that RX lines should have pull-ups on them.

' =========================================================================
'
'   File......
'   Purpose...
'   Author....
'   E-mail....
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' Wait 5 seconds

' Turn on black lights
' Turn on Fog Machine
' Wait 3 seconds

' Turn on Fans
' wait 2 more seconds

' turn off fog
' Trigger uMP3 sound
' Trigger Popup(solenoid) with floods
' Wait 3 seconds

' Turn off Popup
' Wait 5 seconds

' Turn off Fans
' Wait 25 seconds

' Turn off black lights
' Restart sequence


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


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


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

SYMBOL  Sio             = 7                     ' SETUP = UP, no ULN
SYMBOL  RX              = 6                     ' SETUP = UP, no ULN
SYMBOL  TX              = 5                     ' NO ULN
SYMBOL  Trigger         = PIN4                  ' (ULN is pull-down)
SYMBOL  NC3             = 3
SYMBOL  NC2             = 2
SYMBOL  NC1             = 1
SYMBOL  Popup           = PIN0


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

SYMBOL  YES             = 1
SYMBOL  NO              = 0

SYMBOL  TR_ON           = 1                     ' active-high trigger
SYMBOL  TR_OFF          = 0

SYMBOL  IS_ON           = 1                     ' active-high I/O
SYMBOL  IS_OFF          = 0


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

SYMBOL  rc4bits         = B0
SYMBOL   blacklight     =  BIT0
SYMBOL   fog            =  BIT1
SYMBOL   poplights      =  BIT2
SYMBOL   fan            =  BIT3

SYMBOL  timer           = B2                    ' for debounce loop


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

Power_Up:
  rc4bits = %0000                               ' all off
  GOSUB Set_RC4
  PAUSE 5000                                    ' let uMP3 boot up

Reset:
  PINS = %00100000                              ' TX to idle
  DIRS = %00101111                              ' P5, P3..P0 are outputs

  SEROUT TX, T2400, ("ST O 0", 13)              ' loop forever
  PAUSE 25
  SEROUT TX, T2400, ("PC F /BKGROUND.MP3", 13)

  PAUSE 20000                                   ' let PIR warm-up


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

Main:
  timer = 0                                     ' reset debounce timer

Check_Trigger:
  PAUSE 5                                       ' scan delay
  IF Trigger = TR_OFF THEN Main                 ' check trigger input
    timer = timer + 5                           ' update timer
  IF timer < 100 THEN Check_Trigger             ' check timer

  PAUSE 5000

  rc4bits = %0011                               ' fog, blacklight
  GOSUB Set_RC4
  PAUSE 3000

  rc4bits = %1011                               ' fan, fog, blacklight
  GOSUB Set_RC4
  PAUSE 2000

  rc4bits = %1001                               ' fan, blacklight
  GOSUB Set_RC4

  SEROUT TX, T2400, ("ST O 1", 13)              ' play once
  PAUSE 25
  SEROUT TX, T2400, ("PC F /SCREAM.MP3", 13)

  rc4bits = %1101                               ' fan, pop lights, blacklight
  GOSUB Set_RC4
  PopUp = IS_ON
  PAUSE 3000

  rc4bits = %1001                               ' fan, blacklight
  GOSUB Set_RC4
  PopUp = IS_OFF
  PAUSE 5000

  rc4bits = %0001                               ' blacklight
  GOSUB Set_RC4
  PAUSE 25000

  rc4bits = %0000
  GOSUB Set_RC4


  GOTO Reset


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

Set_RC4:
  SEROUT Sio, OT2400, ("!RC4", %11, "S", rc4bits)
  RETURN

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


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


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


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

ChrisBartram

 :) thank you thank you thank you!
Had some of my settings swapped in that code but just now got it working and completed! It will roll out to the "toxic dump" tomorrow!

I'll post a pic or video after we catch our breath!

-Chris