November 22, 2024, 08:52:29 AM

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.


Random Head Banging

Started by Regor, August 05, 2007, 08:17:05 PM

Previous topic - Next topic

Regor

That would be mine against the monitor  :).
I am having a hard time getting my brain wrapped around coding, specifically using RANDOM.   I believe I could have built the mechanical end of a half a dozen elaborate  props in the amount of time I've been trying to figure out programming.  So I'm finally asking for help.

I have a Beast Ejecting Crate that uses 120v solenoids for the pneumatics.  I need the solenoids to be RC4 serial controlled to free up outputs for future additions.  I am trying to get random shaking to the Lidshake and Crateshake cylinders at different intervals for a duration of about 7 seconds, perhaps through a subroutine  (this has been the main cause of bruising to my frontal lobe). 

Here is what I am shooting for:

1.  PIR activated - debounced                                         (I think I have this one figured out)
2.  Light on - from RC4 Kx                                              (light is 120v for now, may be LEDs later)
3.  Smoke on for about 3 seconds - from PIN0                  (switched by small relay)
4.  lid shakes for about 7 seconds - from RC4 Kx                (120v solenoid)
5.  Light off
6.  Pause 5 seconds
7.  Light on
8.  Crate shakes for about 7 seconds - from RC4 Kx           (120v solenoid)
9.  Light off
10. Pause 5 seconds
11. Door opens - from RC4 Kx                                        (120v solenoid)
12. Light on
13. Smoke on for about 3 seconds 
14. Light off
15. Door closes

Would it be possible to shake the lid and crate at the same time (same 7 second routine) maybe after line 10, with another light on and pause line?

I will probably need to play around with the times some and once I figure out the AP8 I will be adding sound.
This will about do it for phase 1.

Thanks so much Jon and everyone.  The posts here have been very informing and inspirational.
Roger

JonnyMac

Here you go, Roger.  You may need to study this a bit, but once what I'm doing clicks in you'll find it's actually quite straightforward.  One thing of note: I am subtracting 29 from the shake timer value -- this is necessary because you're using the RC-4 and it takes the Prop-1 29 milliseconds to send the "S" command.

' =========================================================================
'
'   File...... Roger_MIB.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated... 06 AUG 2007
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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

SYMBOL  Sio             = 7                     ' SETUP = out; no ULN
SYMBOL  PIR             = PIN6
SYMBOL  Smoke           = PIN0


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

SYMBOL  IsOn            = 1
SYMBOL  IsOff           = 0

SYMBOL  IsOpen          = 1
SYMBOL  IsClosed        = 0


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

SYMBOL  relays          = B0
SYMBOL  Light           = BIT0
SYMBOL  Lid             = BIT1
SYMBOL  Crate           = BIT2
SYMBOL  Door            = BIT3

SYMBOL  shkTimer        = B2                    ' shake timer
SYMBOL  delay           = W4
SYMBOL  lottery         = W5                    ' random value


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

Reset:
  PINS = %00000000
  DIRS = %00000001                              ' enable smoke pin

  relays = %0000
  GOSUB Update_Relays

  PAUSE 20000                                   ' PIR warmup / delay


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

Main:
  RANDOM lottery                                ' stir random value
  delay = delay + 10 * PIR                      ' update PIR delay timer
  PAUSE 10                                      ' (match above)
  IF delay < 250 THEN Main                      ' validate PIR signal

  Light = IsOn
  GOSUB Update_Relays
  Smoke = IsOn
  PAUSE 3000
  Smoke = IsOff

  delay = 0                                     ' reset delay timer

Shake_Lid:
  RANDOM lottery
  shkTimer = lottery // 101 + 50                ' 50 to 150 milliseconds
  Lid = 1 - Lid                                 ' flip lid bit
  GOSUB Update_Relays
  shkTimer = shkTimer - 29                      ' remove tx delay
  PAUSE shkTimer
  delay = delay + shkTimer                      ' update delay timer
  IF delay < 7000 THEN Shake_Lid

  Lid = IsOff                                   ' make sure it's closed
  Light = IsOff
  GOSUB Update_Relays
  PAUSE 5000
  Light = IsOn
  GOSUB Update_Relays

  delay = 0

Shake_Crate:
  RANDOM lottery
  shkTimer = lottery // 126 + 75                ' 75 to 200 milliseconds
  Crate = 1 - Crate
  GOSUB Update_Relays
  shkTimer = shkTimer - 29
  PAUSE shkTimer
  delay = delay + shkTimer
  IF delay < 7000 THEN Shake_Crate

  Crate = IsOff                                 ' make sure it's settled
  Light = IsOff
  GOSUB Update_Relays
  PAUSE 5000
  Light = IsOn
  Door = IsOpen
  GOSUB Update_Relays
  Smoke = IsOn
  PAUSE 3000

  GOTO Reset                                    ' everything off


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

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

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


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


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


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


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


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

JonnyMac

August 06, 2007, 09:47:30 AM #2 Last Edit: August 06, 2007, 09:49:59 AM by JonnyMac
Here's a version that shakes both the lid and crate.  The BS1 doesn't have individual bit access to all variables (just B0 and B1), so I use a simple trick to get bits from the random value, lottery.

' =========================================================================
'
'   File...... Roger_MIB_v2.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated... 06 AUG 2007
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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

SYMBOL  Sio             = 7                     ' SETUP = out; no ULN
SYMBOL  PIR             = PIN6
SYMBOL  Smoke           = PIN0


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

SYMBOL  IsOn            = 1
SYMBOL  IsOff           = 0

SYMBOL  IsOpen          = 1
SYMBOL  IsClosed        = 0


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

SYMBOL  relays          = B0
SYMBOL  Light           = BIT0
SYMBOL  Lid             = BIT1
SYMBOL  Crate           = BIT2
SYMBOL  Door            = BIT3

SYMBOL  shkTimer        = B2                    ' shake timer
SYMBOL  delay           = W4
SYMBOL  lottery         = W5                    ' random value
SYMBOL  lottoLo         = B10
SYMBOL  lottoHi         = B11


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

Reset:
  PINS = %00000000
  DIRS = %00000001                              ' enable smoke pin

  relays = %0000
  GOSUB Update_Relays

  PAUSE 20000                                   ' PIR warmup / delay


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

Main:
  RANDOM lottery                                ' stir random value
  delay = delay + 10 * PIR                      ' update PIR delay timer
  PAUSE 10                                      ' (match above)
  IF delay < 250 THEN Main                      ' validate PIR signal

  Light = IsOn
  GOSUB Update_Relays
  Smoke = IsOn
  PAUSE 3000
  Smoke = IsOff

  delay = 0                                     ' reset delay timer

Shake_Lid:
  RANDOM lottery
  shkTimer = lottery // 101 + 50                ' 50 to 150 milliseconds
  Lid = 1 - Lid                                 ' flip lid bit
  GOSUB Update_Relays
  shkTimer = shkTimer - 29                      ' remove tx delay
  PAUSE shkTimer
  delay = delay + shkTimer                      ' update delay timer
  IF delay < 7000 THEN Shake_Lid

  Lid = IsOff                                   ' make sure it's closed
  Light = IsOff
  GOSUB Update_Relays
  PAUSE 5000
  Light = IsOn
  GOSUB Update_Relays

  delay = 0

Shake_Crate:
  RANDOM lottery
  shkTimer = lottery // 126 + 75                ' 75 to 200 milliseconds
  Crate = 1 - Crate
  GOSUB Update_Relays
  shkTimer = shkTimer - 29
  PAUSE shkTimer
  delay = delay + shkTimer
  IF delay < 7000 THEN Shake_Crate

  Crate = IsOff                                 ' make sure it's settled
  Light = IsOff
  GOSUB Update_Relays
  PAUSE 5000
  Light = IsOn
  Door = IsOpen
  GOSUB Update_Relays
  Smoke = IsOn
  PAUSE 3000
  Smoke = IsOff
  Door = IsClosed
  GOSUB Update_Relays
  PAUSE 2000

  delay = 0

Shake_It_Baby:
  RANDOM lottery
  shkTimer = lottery // 51 + 50                 ' 50 to 100 milliseconds
  Lid = lottoLo                                 ' lid = lottery.BIT0
  Crate = lottoHi                               ' crate = lottery.BIT8
  GOSUB Update_Relays
  shkTimer = shkTimer - 29
  PAUSE shkTimer
  delay = delay + shkTimer
  IF delay < 7000 THEN Shake_It_Baby

  GOTO Reset                                    ' everything off


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

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

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


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


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


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


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


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

Regor

Much appreciated, Jon.  You are the Guru of PBASIC voodo.
The program ran great on the Prop1/RC4.  I'll connect to the prop in the next few days and see how it shakes out.  Meanwhile I'll keep studying, I'm determined to learn.
Thanks for a great product and, as always, exceptional support.
Roger

Regor

'After re-reading my first post here, I realized my comments could be misconstrued as the prop1 being hard to use.  This is not true.    There is a bit of a learning curve (as with anything new) for non-programmers, it's just that my curve tends to be rather flat.  I did not mean to imply that the prop1 is difficult to use/program and I encourage anyone that may be intimidated, as I was, by some of the code seen here, to jump in, start simple, and read everything that Jon posts.  It's a lot of fun.  Really. 
Enough rambling and on to my request.

Jon,
Would you take a look at the following program?  I tweaked and added to your original and I wonder if I screwed something up. 

Everything was working great and then last night I was running through the routine (with the air disconnected) to check timing, sound, etc. and I noticed the LED circuit from PIN0 was rapidly cycling on and off.  I thought, "Wow,that's actually kinda cool."  After   running the routine several more times the circuit died.  I realize this is probably just a bad LED in the circuit and since I was needing to leave for work soon I decided I would check it later.  Continuing on, I connected the air, triggered the prop, and everything else worked except the relays.  Nothing at the RC4. I switched the power off for a while and then back on and still nothing at the RC4.  Everything else (sound, remaining LED circuits, fog) worked perfectly.  About the fourth try things really got weird.  I switched the power back on and every relay closed in.  Now, I have been working on this prop for quite awhile and I didn't think that it would ever scare me but when all four cylinders fired at once, along with the lights and sound (that AP8 is quite loud), well, I stand corrected.      After picking myself up and reassuring my wife that "that explosion type sound was nothing", I disconnected the air and switched the power off.  Trying again several times (with the air off) got the same results.  I reloaded the program and connected back up and it seemed to be working OK.  I only ran it a few times since I needed to leave for work but everything seemed normal.  After that long winded account my question is this.  Did I introduce something into the program that might have caused erratic behavior in the RC4?  Think it may happen again?  I haven't had a chance to work with it this morning seeing as how I really need to get some sleep but I'll try again this afternoon.

Any thoughts, suggestions, or kind words will be appreciated.

Roger       


=========================================================================
'
'   File......Roger_MIB.BS1
'   Purpose...
'   Author....Jon Williams EFX-TEK
'   E-mail....jwilliams@efx-tek.com
'   Started...
'   Updated...06 AUG 2007
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================

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


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

'  11 SEPT 2007   Changed light to 12v LED's
'  22 SEPT 2007   Added second LED circuit
'  17 OCT 2007    Added beast
'  19 OCT 2007    Added door LED's
'  28 OCT 2007    Added AP8, changed timing

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

                                                     ' power switch position 2
SYMBOL  Sio               = 7                ' SETUP = out, ULN 2003A
SYMBOL  PIR               = PIN6
SYMBOL  Doorlight       = PIN3          ' 5 LED's, black wire, OUT 3
SYMBOL  Smoke          = PIN2          ' white wire from relay, OUT 2
SYMBOL  Light             = PIN1          ' 5 LED's,  blue wire, OUT 1
SYMBOL  Glow             = PIN0         ' 6 LED's,  white wire, OUT 0


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

SYMBOL  Baud             = OT2400

SYMBOL  IsOn             = 1
SYMBOL  IsOff            = 0

SYMBOL  IsOpen           = 1
SYMBOL  IsClosed         = 0

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

SYMBOL  relays           = B0
SYMBOL  Lid               = BIT0          ' K1, solenoid B
SYMBOL  Crate            = BIT1         ' K2, solenoid C
SYMBOL  Door             = BIT2         ' K3, solenoid D
SYMBOL  Beast            = BIT3         ' K4, solenoid A

SYMBOL  shkTimer        = B2           ' shake timer
SYMBOL  delay            = W4
SYMBOL  lottery          = W5           ' random value

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

Reset:
  PINS = %00000000
  DIRS = %00001111                           ' enable smoke,light, glow, door outputs

  SEROUT SIO, BAUD, ("!!!!!!AP8", %00, "X")          ' stop audio if playing

  relays = %0000
  GOSUB Update_Relays

  PAUSE 20000                                ' PIR warmup / delay

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

Main:
  RANDOM lottery                            ' stir random value
  delay = delay + 10 * PIR                ' update PIR delay timer
  PAUSE 10                                    ' (match above)
  IF delay < 250 THEN Main              ' validate PIR signal

  Start_Audio:
    SEROUT SIO, BAUD, ("!AP8", %00, "P", 0)

  PAUSE 3000
  Glow  = IsOn
  Smoke = IsOn
  PAUSE 4000
  Smoke = IsOff
  Light = IsOn

  delay = 0                                       ' reset delay timer

Shake_Lid:
  RANDOM lottery
  shkTimer = lottery // 101 + 50         ' 50 to 150 milliseconds
  Lid = 1 - Lid                                   ' flip lid bit
  GOSUB Update_Relays
  shkTimer = shkTimer - 29                ' remove tx delay
  PAUSE shkTimer
  delay = delay + shkTimer                 ' update delay timer
  IF delay < 8000 THEN Shake_Lid

  Lid = IsOff                                     ' make sure it is closed
  Light = IsOff
  GOSUB Update_Relays
  PAUSE 7000
  Light = IsOn

  delay = 0

Shake_Crate:
  RANDOM lottery
  shkTimer = lottery // 126 + 75        ' 75 to 200 milliseconds
  Crate = 1 - Crate                            ' flip crate bit
  GOSUB Update_Relays
  shkTimer = shkTimer - 29
  PAUSE shkTimer
  delay = delay + shkTimer
  IF delay < 8000 THEN Shake_Crate

  Crate = IsOff                                ' make sure it's settled
  Light = IsOff
  GOSUB Update_Relays
  PAUSE 7000
  Light = IsOn
  Door = IsOpen                             ' open door
  GOSUB Update_Relays
  Smoke = IsOn
  PAUSE 600
  Beast = IsON                               ' extend beast
  GOSUB Update_Relays
  Doorlight = IsOn                          ' illuminate beast
  Smoke = IsOff
  PAUSE 8000
  Beast = IsOff                              ' retract beast
  GOSUB Update_Relays
  PAUSE 2000

  GOTO Reset                             ' everything off

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

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

JonnyMac

First... this is a very sophisticated program as Prop-1 programs go.  I've been coding the BS1 since 1994 and this was not one that I was able to pound out in a few minutes (as is what happens with most requests) -- this on took some time.  Don't be too hard on yourself for not fulling understanding it at first blush.

My guess, based on your description, is that you're running the ULN on the ragged edge and that once it overheats it acts up.  I ran the program on my desk with just LEDs connected to the OUTx terminals and there was no on-and-off cycling as you observed (and, based on the program there shouldn't be).

Another possibility -- if the ULN has not been over-taxed -- is that you're overloading the power supply that is driving the relays and other devices powered by OUTx; you may want to check on this.  Or... if you've extend wires, did you size the wire appropriately for the current being carried (this is a frequent problem).

I hope that one or more of these hints gets you back on track.
Jon McPhalen
EFX-TEK Hollywood Office