November 23, 2024, 10:57:49 PM

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.


Help with Random Timer for Spider Wiper Pinchers

Started by Jaxombie, August 06, 2011, 03:28:40 PM

Previous topic - Next topic

Jaxombie

See code below.  I am using a Prop-1 to controller the pinchers (jaws) of a spider wiper prop (will also control LED eyes and a 120 volt relay for the wiper motor, too).  My issue is with the following code in that it is not creating enough of a delay and the pinchers are going off alot more than I want.  I would like for the delay time to be between a full one second to five seconds.  Can someone help?

Main:

    RANDOM lottery                                ' re-stir
    delay = MaxJump - MinJump + 1                 ' get span
    delay = lottery // delay + MinJump            ' calc jump timing
    ptoggle = IsOn - ptoggle                      ' toggle cylinders
    PAUSE delay                                   ' I want this to be from 1 to 5 seconds
    IF ptoggle = 0 THEN Pincher_IsOn
    HIGH Pinchers
    DEBUG PIN1, delay, lottery, CR, CR
    GOTO Main

Pincher_IsOn:
    LOW Pinchers
    DEBUG PIN1, delay, lottery, CR, CR
    GOTO Main

JonnyMac

You're not showing the whole program -- you should have constants defined for MinJump and MaxJump; they would be 1000 (1s) and 5000 (5s), respectively.  Put this in your program with the other constants.

SYMBOL  MinJump         = 1000
SYMBOL  MaxJump         = 5000


And make sure that delay is a 'W' variable, not a 'B' variable.  If post or attache the whole program it will be helpful.
Jon McPhalen
EFX-TEK Hollywood Office

Jaxombie

Here is the complete program.  I have udpated the delay variable to be a word (w) versus a byte.

' =========================================================================
'
'   File...... Spider Wiper Dark Phobia Random 2011 1.0.BS1
'   Purpose... Control pinchers on Spider Wiper
'   Author.... Jason (Jaxombie) Kane
'   E-mail.... jaxombie@gmail.com
'   Started... 08/06/2011
'   Updated... 08/07/2011
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------
' To control and simulate the chomping action of a spider's mandibles/pinchers
' Pin0 on Prop-1 does not work

' -----[ Revision History ]------------------------------------------------
' August 7th, 2011 - Updated delay value to a word versus a byte

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

'SYMBOL  Sio            = 7                     ' SETUP = out; no ULN
SYMBOL  Pinchers        = 1


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

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

SYMBOL  MinJump         =  1000              ' timing for lid/box jumps
SYMBOL  MaxJump         =  5000

SYMBOL  Baud            = OT2400


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

SYMBOL  sensors         = B0                    ' sensor bits
SYMBOL  timer           = B1                    ' debounce timer
SYMBOL  mask            = B2                    ' inputs mask
SYMBOL  lottery         = B3                    ' random value
SYMBOL  ptoggle         = B4                    ' toggle value for pinchers
SYMBOL  delay           = W3                    ' stores random delay value

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

Reset:
  DIRS = %01111111                              ' all outputs except for 0 and 7 are inputs

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

Main:

    RANDOM lottery                                ' re-stir
    delay = MaxJump - MinJump + 1                 ' get span
    delay = lottery // delay + MinJump            ' calc jump timing
    ptoggle = IsOn - ptoggle                      ' toggle cylinders
    PAUSE delay                                   ' I want this to be from 1 to 5 seconds
    IF ptoggle = 0 THEN Pincher_IsOn
    HIGH Pinchers
    DEBUG PIN1, delay, lottery, CR, CR
    GOTO Main

Pincher_IsOn:
    LOW Pinchers
    DEBUG PIN1, delay, lottery, CR, CR
    GOTO Main

JonnyMac

You need to make lottery a "W" variable as well. You also have unused variables -- will the program do more later?
Jon McPhalen
EFX-TEK Hollywood Office

bsnut

This is what Jon is talking about and needs to look like this
SYMBOL lottery = W4


I also notice that, you are using the variable "ptoggle" as a bit. If you are, then it needs to be setup like this.

SYMBOL ptoggle = BIT0
William Stefan
The Basic Stamp Nut

JonnyMac

Just be careful; BIT0 and B0 occupy the same RAM space.  If you're not using the sensors, timer, and mask variables they should be removed from the listing.

It's a good idea to keep you listings very human friendly; don't be afraid of white space (extra blank lines) to improve readability and labels that help others understand the code.  Here's a simplified version of your program

' =========================================================================
'
'   File...... Spider Wiper Dark Phobia Random 2011 1.0.BS1
'   Purpose... Control pinchers on Spider Wiper
'   Author.... Jason (Jaxombie) Kane
'   E-mail.... jaxombie@gmail.com
'   Started... 08/06/2011
'   Updated... 08/07/2011
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------
'
' To control and simulate the chomping action of a spider's mandibles /
' pinchers.
'
' Pin0 on Prop-1 does not work

' -----[ Revision History ]------------------------------------------------
'
' August 7th, 2011 - Updated delay value to a word versus a byte
' August 8th, 2011 - Updated by Jon Williams

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

SYMBOL  Pinchers        = PIN1                  ' use P1 or OUT1


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

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

SYMBOL  MinJump         = 1000                  ' timing for lid/box jumps
SYMBOL  MaxJump         = 5000


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

SYMBOL  delay           = W4                    ' stores random delay value
SYMBOL  lottery         = W5                    ' random #


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

Reset:
  DIRS = %01111110                              ' 0, 7 = I, 2..6 = O

  lottery = 1031                                ' initialize random #

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

Main:
  RANDOM lottery                                ' re-stir
  delay = MaxJump - MinJump + 1                 ' get span
  delay = lottery // delay + MinJump            ' calc jump timing
  PAUSE delay                                   ' 1s to 5s delay

  Pinchers = IsOn - Pinchers                    ' toggle output

Report:
  DEBUG PIN1, delay, lottery, CR, CR
  GOTO Main
Jon McPhalen
EFX-TEK Hollywood Office

Jaxombie

Thanks so much for your help, Jon.  I really appreciate it.