November 23, 2024, 07:14:35 PM

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.


Random solenoid firing

Started by imagineerdan, October 17, 2016, 05:42:57 PM

Previous topic - Next topic

imagineerdan

Hello, I have 4 solenoids in the gag I need to fire at random and at random intervals. Its for 4 pop ups that I want to just go on a loop at random over and over again. What would the code look like for this on a prop-1?

I am trying to work something out but all my codes are for LEDs with PWM which I done need. Thanks!

JackMan

You're gonna have to provide a lot more details on exactly how you want this to execute. How long each solenoid needs to be active, the minimum/maximum time between random activation, if activation of any particular solenoid can repeat before the other 3 have activated, etc. The more specifics you can provide, the easier it is for someone to code it.

JonnyMac

Jack is right: and the root word of specification is specific! :)

Still, I put together a framework that you can start with and tune to your needs. This only allows one output on at the same time, and all have to pop in a cycle before there are any repeats; this keeps the distribution even.

' =========================================================================
'
'   File...... random_poppers.bs1
'   Purpose...
'   Author.... JonnyMac
'   E-mail....
'   Started...
'   Updated... 17 OCT 2016
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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

SYMBOL  Pop3            = 3
SYMBOL  Pop2            = 2
SYMBOL  Pop1            = 1
SYMBOL  Pop0            = 0


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

SYMBOL  Yes             = 1
SYMBOL  No              = 0

SYMBOL  IsOn            = 1                     ' active-high I/O
SYMBOL  IsOff           = 0


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

SYMBOL  thePin          = B2
SYMBOL  last            = B3
SYMBOL  mask            = B4
SYMBOL  check           = B5
SYMBOL  played          = B6

SYMBOL  millis          = W4
SYMBOL  lottery         = W5
SYMBOL   lottoLo        =  B10
SYMBOL   lottoHi        =  B11


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

Power_Up:
  READ 4, lottoHi                               ' read saved seed
  READ 5, lottoLo
  last = -1                                     ' let abt pin any be first

Reset:
  PINS = %00000000                              ' all off
  DIRS = %00001111                              ' P3..P0 are outputs


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

Main:
  RANDOM lottery

Get_Pin:
  thePin = lottery & %11                        ' 0..3
  IF thePin = last THEN Main                    ' no repeats
  READ thePin, mask                             ' convert pin to mask
  check = played & mask                         ' check against playlist
  IF check > 0 THEN Main                        ' try again if already played
    last = thePin                               ' save for next cycle

Pop_Up:
  RANDOM lottery
  millis = lottery // 501 + 500                 ' 0.5 to 1.0s
  HIGH thePin
  PAUSE millis
  LOW thePIN

Pop_Delay:
  RANDOM lottery
  millis = lottery // 4001 + 1000               ' 1s to 5s
  PAUSE millis

  played = played | mask
  IF played <> %1111 THEN Main

Reset_Played:
  played = %0000                                ' clear playlist
  WRITE 4, lottoHi                              ' save seed
  WRITE 5, lottoLo

  GOTO Main


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


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


' -----[ User Data ]-------------------------------------------------------

PinMasks:
  EEPROM (%0001, %0010, %0100, %1000)

Seed:
  EEPROM (10, 31)                               ' Addresses = 4 & 5
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

Thanks guys absolutely can be more specific. I have this gag in our Haunted House where were the guest walks into a room and there are fake bugs everywhere. Non of them are moving except on one wall I have four electromagnetic solenoids rigged up to make these big spiders flick around. I would like to have a program which basically makes those four bugs move at random constantly moving around. It would also be nice if there was a variable which I could turn up and down the frequency or active-ness of the spiders?

thanks soo much!

JonnyMac

At some point, you're going to have to dive in and write or edit some code -- it's impossible for anyone to write the "perfect" program without having access to the hardware under control.

I added the POT command to your program to change the duration between pulsed outputs, and I shortened the pulse duration. Again, without having the physical hardware to test, these are just guesses on my part.

It's on you now, Dan. Have fun!

' =========================================================================
'
'   File...... random_poppers.bs1
'   Purpose...
'   Author.... JonnyMac
'   E-mail....
'   Started...
'   Updated... 18 OCT 2016
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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

SYMBOL  Speed           = 7                     ' no SETUP, no ULN

SYMBOL  Pop3            = 3
SYMBOL  Pop2            = 2
SYMBOL  Pop1            = 1
SYMBOL  Pop0            = 0


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

SYMBOL  Yes             = 1
SYMBOL  No              = 0

SYMBOL  IsOn            = 1                     ' active-high I/O
SYMBOL  IsOff           = 0


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

SYMBOL  thePin          = B2
SYMBOL  last            = B3
SYMBOL  mask            = B4
SYMBOL  check           = B5
SYMBOL  played          = B6

SYMBOL  millis          = W4
SYMBOL  lottery         = W5
SYMBOL   lottoLo        =  B10
SYMBOL   lottoHi        =  B11


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

Power_Up:
  READ 4, lottoHi                               ' read saved seed
  READ 5, lottoLo
  last = -1                                     ' let abt pin any be first

Reset:
  PINS = %00000000                              ' all off
  DIRS = %00001111                              ' P3..P0 are outputs


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

Main:
  RANDOM lottery

Get_Pin:
  thePin = lottery & %11                        ' 0..3
  IF thePin = last THEN Main                    ' no repeats
  READ thePin, mask                             ' convert pin to mask
  check = played & mask                         ' check against playlist
  IF check > 0 THEN Main                        ' try again if already played
    last = thePin                               ' save for next cycle

Pop_Up:
  RANDOM lottery
  millis = lottery // 401 + 100                 ' 0.1 to 0.5s
  HIGH thePin
  PAUSE millis
  LOW thePIN

Pop_Delay:
  POT Speed, 114, millis                        ' read pot
  millis = millis * 108 / 10 + 250              ' scale: 250 to 3000 ms
  PAUSE millis

Check_Played:
  played = played | mask
  IF played <> %1111 THEN Main

Reset_Played:
  played = %0000                                ' clear playlist
  WRITE 4, lottoHi                              ' save seed
  WRITE 5, lottoLo

  GOTO Main


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


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


' -----[ User Data ]-------------------------------------------------------

PinMasks:
  EEPROM (%0001, %0010, %0100, %1000)

Seed:
  EEPROM (10, 31)                               ' Addresses = 4 & 5
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

Great thanks soo much, I took the code you originally posted and played with the durations and really got a perfect custom code! Thanks soo much for getting me started talk soon!