November 23, 2024, 02:57:52 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.


Looking for experienced eyes to review my first code attempt for Prop-1

Started by ZombieDad, January 03, 2019, 10:38:46 AM

Previous topic - Next topic

ZombieDad

Hey Folks,

I've finally managed to put together some hardware (don't have the audio board yet, so haven't tested that, but the relays are working so I should be good) and written my first code for the Prop-1 stealing liberally from this site. I'm hoping someone with experience can take a look and offer any improvement suggestions. It seems to be working, but I may have totally hacked some of this up... I tried to put descriptions of the intent and the hardware at the start of the code. Sorry the copy/paste seemed to mess up the tabbing... I also attached the actual BS1 file. Thanks in advance!

' ========================================================================================
'
'   File...... CoffinV1R2.BS1
'   Purpose... Run Coffin Halloween Prop
'   Started... 27 DEC 2018
'   Updated... 03 JAN 2019
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' ========================================================================================

' -----[ Program Description ]------------------------------------------------------------
'  Prop-1, PIR, FN-BC04-TB, SainSmart 4-Channel Relay Module, and EFX-TEK solenoid
'  Upon sensing motion:
'    Turn on light inside coffin
'    Trigger one of three audio files
'    Open and close coffin lid to simulate monster trying to escape

' -----[ Revision History ]---------------------------------------------------------------
'  V1 = adjusting to use the FN-BC04-TB instead of a commercial MP3 player and speaker

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

SYMBOL  PIR         =   PIN6                ' SETUP = DN (or out) on Prop-1 board; INPUT from motion sensor
SYMBOL  Light      =   PIN0                ' Light on Relay1 is connected to Pin0
SYMBOL  SoundFX1   =   PIN1                ' Audio Trigger1 on Relay2 is connected to Pin1
SYMBOL  SoundFX2   =   PIN2                ' Audio Trigger2 on Relay3 is connected to Pin2
SYMBOL  SoundFX3   =   PIN3                ' Audio Trigger3 on Relay4 is connected to Pin3
SYMBOL  Lid         =   PIN4              ' 12V OUTPUT for cylinder is connected to V+/Out4

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

SYMBOL  IsOn        =   0                   ' For active-Low in/out
SYMBOL  IsOff       =   1                   ' For active-Low in/out
SYMBOL  ScanDelay   =   10                  ' 10ms scan delay; use 5 to 10ms for best results

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

SYMBOL  pirTimer        = B2                 ' For debouncing PIR sensor
SYMBOL  audioChooser     = B3              ' To pick between various audio files
SYMBOL  shkTimer        = W2                ' Length of time to keep Lid open/close
SYMBOL  delay           = W3              ' Timer for how long to toggle lid open/close
SYMBOL  lottery         = W4                ' random value
SYMBOL  showLength       = W5              ' Set how long to run show

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

audioChooser = 1                          ' set audio file selection variable to first file

' =======  [ Main Code ] =================================================================

Reset:
  PINS = %00001111                           ' all off (0-3 are active-low, 4 is active-high)
  DIRS = %00011111                           ' P4..P0 are outputs
  PAUSE 20000                                ' warm-up / inter-show delay; note: change to 45s when code is complete
  pirTimer = 0                               ' clear timer for PIR scan
  delay = 0                              ' clear timer for lid motion

Main:
  RANDOM lottery                            ' stir random value
  PAUSE ScanDelay                          ' PIR trigger test
  pirTimer = pirTimer + ScanDelay * PIR       ' advance/clear timer
  IF pirTimer < 250 THEN Main              ' minimize false positives
  Light = IsOn                              ' Turn on light in coffin
  IF audioChooser = 1 THEN Show1            ' different audio file for each "show"
  IF audioChooser = 2 THEN Show2            ' different audio file for each "show"
  IF audioChooser = 3 THEN Show3            ' different audio file for each "show"
  DEBUG "Something went wrong", CR         ' This should never show; note: remove when code is complete

Show1:
  SoundFX1 = IsOn                          ' Trigger first audio file
  showLength = 5000                       ' Length of audio file #1
  audioChooser = 2                       ' Move audioChooser to second file
  GOTO Shake_Lid

Show2:
  SoundFX2 = IsOn                          ' Trigger second audio file
  showLength = 7000                       ' Length of audio file #2
  audioChooser = 3                       ' Move audioChooser TO third file
  GOTO Shake_Lid

Show3:
  SoundFX3 = IsOn                          ' Trigger third audio file
  showLength = 10000                     ' Length of audio file #3
  audioChooser = 1                       ' Move audioChooser to first file
  GOTO Shake_Lid

Shake_Lid:
  RANDOM lottery
  shkTimer = lottery // 751 + 250            ' 250 to 1000 milliseconds; note: optimize during testing
  Lid = 1 - Lid                            ' Flip lid output to open or close lid
  PAUSE shkTimer                          ' Leave lid in state for random time
  delay = delay + shkTimer                ' Increase delay timer
  IF delay < showLength THEN Shake_Lid       ' Keeping cycling lid until audio file is done
  GOTO Reset                                ' Return to Reset to start over for next motion trigger event

ZombieDad

I'm still poking around on the various PBASIC sites and just found the "BRANCH" command so looks like I could reduce those three IF ... THEN statements down to one line by using BRANCH and adjusting the values from 1,2,3 to 0,1,2...

1) replace the current line in the Initialization section with:
audioChooser = 0

2) replace the three IF...THEN statements with:
BRANCH audioChooser (Show1, Show2, Show3)

3) reduce each of the audioChooser increment statements (in Show1, Show2, Show3) by 1: (using the statement in Show1 as an example)
audioChooser = 1                       ' Move audioChooser to second file

Jeff Haas

Hi Matt,

I tested your code on the Prop-1 with the Prop-1 trainer.  I looked up the sound module, and it seems like you're using the 4-relay module to turn on the light in the coffin and trigger the sounds (instead of buttons the sound module suggests).  But I'm not sure what you're using the rattle the lid of the coffin.  And it looks like you got one of those relay modules that requires you to drive each relay low to turn it on instead of high - totally annoying, but oh well.

However, the first version of the code you posted seems to work fine!  You have plenty of free memory space left so unless you plan on adding a lot of stuff to this, you can leave the code as it is.  (To see how much memory is taken up, in the Basic Stamp editor, go to the Run menu and pick Memory Map.)

The main thing will be to see how the whole thing works when hooked up to the prop.  The banging of the lid will be a key part to tune, and that will depend on what you built.

Jeff

ZombieDad

Hi Jeff,

Thanks for the response! I have an air cylinder run by a 12V solenoid to open and close the coffin lid (just a few inches) but completely agree that I'll have to tune the timing of that randomizer once I have it all hooked up. I also just noticed that I can probably control the sound module via serial communications which would just use one I/O channel and allow direct control instead of having to go through the relays. Regarding the relays, I didn't realize I was ordering a drive low relay and that was definitely annoying as it took me a bit to figure out what was going on with the code...

I also really appreciate the tip on how to check the memory use - I wasn't sure how to do that so thanks!

I'll post again later after I get it all hooked up and/or if I decide to try out the serial communication and need help there.

Matt

Quote from: Jeff Haas on January 07, 2019, 07:30:58 PM
Hi Matt,

I tested your code on the Prop-1 with the Prop-1 trainer.  I looked up the sound module, and it seems like you're using the 4-relay module to turn on the light in the coffin and trigger the sounds (instead of buttons the sound module suggests).  But I'm not sure what you're using the rattle the lid of the coffin.  And it looks like you got one of those relay modules that requires you to drive each relay low to turn it on instead of high - totally annoying, but oh well.

However, the first version of the code you posted seems to work fine!  You have plenty of free memory space left so unless you plan on adding a lot of stuff to this, you can leave the code as it is.  (To see how much memory is taken up, in the Basic Stamp editor, go to the Run menu and pick Memory Map.)

The main thing will be to see how the whole thing works when hooked up to the prop.  The banging of the lid will be a key part to tune, and that will depend on what you built.

Jeff

Jeff Haas

Unfortunately, you won't be able to use the serial mode, the website shows that board only accepts 9600 baud, and the Prop-1 only does serial at 2400 baud.

ZombieDad

Thanks Jeff, you saved me a ton of work since I wouldn't have even though to check the baud rate compatibility until after playing around and trying to learn the commands!

If you have time for one last question - you mentioned that I was using relays to trigger the sound board instead of buttons. To be honest, I planned to use relays since I figured they were necessary but is there a better way to mimic the "button" action directly from the Prop-1? My assumption is that the audio board is providing voltage (I am planning to power both the audio board and the prop-1 via 12-V power supplies) to those screw terminals looking for the circuits to close and I was afraid of frying something on the Prop-1 by providing voltage from the audio board to the Prop-1. Is there as safe way to wire the audio board directly to the Prop-1 to trigger the 4 audio channels?

I'm sure the terms I am using are bad, but I'm a chemical engineer not an electrical engineer so I only know enough of this stuff to be dangerous... :-)

Jeff Haas

For this kind of audio board, we don't really know the specs on it. So I would suggest sticking with the relays, since they're the same thing as the buttons they suggest.  They also provide electrical isolation, to keep the two boards separate from each other.