November 22, 2024, 09:44:36 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.


Using RANDOM

Started by CraigN, October 21, 2009, 06:13:14 PM

Previous topic - Next topic

CraigN

Hi Jon,

I'm not sure of how RANDOM works and do not currently use it in my project code.
 
I've completed my Halloween project which has a skeleton sitting in a Outhouse I built. (link follows: http://www.garageofevilnetwork.com/xn/detail/2243951:Video:57884?xg_source=activity)

The existing code works fine and fires a fog machine, opens the door, turns on lights, starts the audio program on a CAR/P 300 etc, turns off lights and closes door.

I have a wiper motor moving an arm of the skeleton that I have turning on and off throughout the program. I would like the wiper motor to be a bit more "intermittent" rather the "on" for 4 seconds "off" for 10 seconds, repeat. Note: the video was done before the wiper motor was added to the code and is constantly "on".

Would random accomplish this? by this I mean start/stop the wiper motor based on random timing?

I read the forum section on RANDOM but am not sure I understand it.

Regards,
Haunting in Ohio.

JonnyMac

RANDOM could help.  Why don't you post your code so I can add random motor control to it -- just give me the specifics of how random you want it to be.
Jon McPhalen
EFX-TEK Hollywood Office

CraigN

Thanks Jon. 

The code is on another computer and I will post it in the morning.

Thank for your help (again).

Also, I am very impressed by how much help you give to people who write to you and how much patience you have. 

You serve your company well.

Regards,

CraigN

CraigN

Jon,

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


' -----[ Program Description ]---------------------------------------------
'
' Trigger :: Parallax-compatible PIR or N.O. button (mat switch, etc.)
'            -- connect N.O. button between P6.W and P6.R


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


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

SYMBOL  Trigger         = PIN6                  ' SETUP = DN
SYMBOL  CARP            = PIN5                  ' V+/OUT5 to V-Trig
SYMBOL  Motor           = PIN2                  ' V+/OUT2
SYMBOL  fogger          = PIN3                  ' V+/OUT3
SYMBOL  Light           = PIN1                  ' V+/OUT1
SYMBOL  Doors           = PIN0                  ' V+/OUT0


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

SYMBOL  IsOn            = 1                     ' for active-high in/out
SYMBOL  IsOff           = 0

SYMBOL  IsOpen          = 1
SYMBOL  IsClosed        = 0

SYMBOL  Yes             = 1
SYMBOL  No              = 0

SYMBOL  Baud            = OT2400                ' B/R jumper removed


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

SYMBOL  timer           = B2


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

Reset:
  PINS = %00000000                              ' clear all
  DIRS = %00101111                              ' set output pins

  PAUSE 30000                                    ' let PIR warmp-up


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

Main:
  timer = 0                                     ' reset timer

Check_Trigger:
  PAUSE 5                                      ' loop pad
  timer = timer + 5 * Trigger                   ' update timer
  IF timer < 250 THEN Check_Trigger             ' wait for 0.1 sec input

  fogger = IsOn
  PAUSE 5000
  fogger = IsOff

  Doors = IsOpen
                                            ' let doors fully open
  PAUSE 1000

  Light = IsOn
  PAUSE 1000

Start_Audio:
  CARP = IsOn
  PAUSE 250
  CARP = IsOff

  Motor = IsOn
  PAUSE 4000
  Motor = IsOff

  PAUSE 10000

  Motor = IsOn
  PAUSE 4000
  Motor = IsOff

  PAUSE 10000

  fogger = IsOn
  PAUSE 2000
  fogger = IsOff

  Motor = IsOn
  PAUSE 4000
  Motor = IsOff

  PAUSE 5000

  fogger = IsOn
  PAUSE 2000
  fogger = IsOff

  Motor = IsOn
  PAUSE 6000
  Motor = IsOff

  PAUSE 20000                                   ' adjust for audio + 5s (5000)

  fogger = IsOn
  PAUSE 2000
  fogger = IsOff

  PAUSE 5000

  Motor = IsOn
  PAUSE 3000
  Motor = IsOff

  PAUSE 2000

  Motor = IsOn
  PAUSE 4000
  Motor = IsOff

  Light = IsOff
  PAUSE 1000                                     ' dramatic pause

                                                 ' let doors fully open
  Doors = IsClosed

  PAUSE 2000

Show_Delay:
  PAUSE 30000                                     ' 30s + 30s = 1min
  GOTO Reset


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


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


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


Regards,

CraigN

JonnyMac

October 23, 2009, 08:52:53 AM #4 Last Edit: October 23, 2009, 09:50:45 AM by JonnyMac
Give this one a try, Craig.  It randomizes the motor and fogger events.  Note, though, that within the Pgm_Loop sections (ends with Update_Timers) everything is in 100ms units -- I had to do this because of the duration of your audio (about 2 minutes based on the accumulated delays of the original program).

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


' -----[ Program Description ]---------------------------------------------
'
' Trigger :: Parallax-compatible PIR or N.O. button (mat switch, etc.)
'            -- connect N.O. button between P6.W and P6.R


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


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

SYMBOL  Trigger         = PIN6                  ' SETUP = DN
SYMBOL  CARP            = PIN5                  ' V+/OUT5 to V-Trig
SYMBOL  Fogger          = PIN3                  ' V+/OUT3
SYMBOL  Motor           = PIN2                  ' V+/OUT2
SYMBOL  Light           = PIN1                  ' V+/OUT1
SYMBOL  Doors           = PIN0                  ' V+/OUT0


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

SYMBOL  IsOn            = 1                     ' for active-high in/out
SYMBOL  IsOff           = 0

SYMBOL  IsOpen          = 1
SYMBOL  IsClosed        = 0

SYMBOL  Yes             = 1
SYMBOL  No              = 0


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

SYMBOL  fTimer          = W2                    ' fogger timer
SYMBOL  mTimer          = W3                    ' motor timer
SYMBOL  pTimer          = W4                    ' program timer
SYMBOL  lottery         = W5                    ' random #


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

Reset:
 PINS = %00000000                              ' clear all
 DIRS = %00101111                              ' set output pins

 PAUSE 30000                                   ' let PIR warmp-up


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

Main:
 pTimer = 0                                    ' reset timer

Check_Trigger:
 RANDOM lottery
 PAUSE 5                                       ' loop pad
 pTimer = pTimer + 5 * Trigger                 ' update timer
 IF pTimer < 250 THEN Check_Trigger            ' wait for 0.25 sec input

 Fogger = IsOn
 PAUSE 5000
 Fogger = IsOff

 Doors = IsOpen                                ' let doors fully open
 PAUSE 1000

 Light = IsOn
 PAUSE 1000

Start_Audio:
 CARP = IsOn
 PAUSE 100
 CARP = IsOff

 pTimer = 1130                                 ' set program time (0.1s units)


Pgm_Loop:
 PAUSE 95                                      ' for 100ms loop


Check_Motor:
 IF mTimer > 0 THEN Check_Fogger
   Motor = 1 - Motor                           ' togger motor output
   IF Motor = IsOff THEN Motor_Off

Motor_On:
 RANDOM lottery
 mTimer = lottery // 21 + 20                   ' 20 to 40 = 2 to 4 secs
 GOTO Check_Fogger

Motor_Off:
 RANDOM lottery
 mTimer = lottery // 61 + 60                   ' 60 to 120 = 6 to 12 secs


Check_Fogger:
 IF fTimer > 0 THEN Update_Timers
   Fogger = 1 - Fogger                         ' flip fogger state
   IF Fogger = IsOff THEN Fog_Off

Fog_On:
 RANDOM lottery
 fTimer = lottery // 21 + 10                   ' 10 to 30 = 1 to 3 secs
 GOTO Update_Timers

Fog_Off:
 RANDOM lottery
 fTimer = lottery // 71 + 80                   ' 80 to 150 = 8 to 15 secs


Update_Timers:
 mTimer = mTimer - 1
 fTimer = fTimer - 1
 pTimer = pTimer - 1
 IF pTimer > 0 THEN Pgm_Loop


Done:
  Motor = IsOff
  Fogger = IsOff
  Light = IsOff
  PAUSE 1000                                    ' dramatic pause

 Doors = IsClosed                              ' let doors fully open
 PAUSE 2000

Show_Delay:
 PAUSE 30000                                   ' 30s + 30s = 1min
 GOTO Reset


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


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


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


Jon McPhalen
EFX-TEK Hollywood Office

CraigN

Thanks Jon,

I'll try this tonight and let you know how it works out.

CraigN