November 23, 2024, 09:39:04 AM

News:

Got VSA?  Want to use your Prop-SX?  Now you can!  See the VSA section of the Library forum for Prop-SX code that works with VSA.


Easy code problem with the PROP1 contoller with PIN0

Started by greendonkey, September 26, 2009, 03:39:55 PM

Previous topic - Next topic

greendonkey

Hello,

While I'm not to total newbie, I am a novice programming these controllers. I haven't touched one since last year too!

Anyway, I have a very perplexing problem. I'm trying to make a dummies body jerk, and the head to shake. It'd be really cool if I could get the body to jerk in a radom fashon too, but the problem is that the HeadShake is either stuck On or Off no matter what I do! I tried moving this variable to PIN1 and PIN0. While at PIN0 it stays off, and at PIN1 it stays on. I also thought it might be a memory overlay problem and changed my counters from Bx to Wx.

Any help you could suggest would be greatly appreiated!!
Thanks,
Chris


------------------
' =========================================================================
'
'   File...... Chair Torture.BS1
'   Purpose...
'   Author.... Chris Villanueva
'   E-mail.... Chris@Vspot.net
'   Started... 2009-09-26
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================

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


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


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

SYMBOL  BodyJump        = PIN1                  ' use V+/OUT0
SYMBOL  HeadShake       = PIN0                  ' use V+/OUT0
SYMBOL  Trigger         = PIN6                  ' SETUP = DN

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

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

SYMBOL  Yes             = 1
SYMBOL  No              = 0


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

SYMBOL  jumpMax         = W0
SYMBOL  jumps           = W1
SYMBOL  twitchMax       = W2
SYMBOL  twitch          = W3

SYMBOL  timer           = W4
SYMBOL  lottery         = W5


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

Reset:
  PINS = %00000000                              ' clear all
  DIRS = %00000001                              ' set outputs

  PAUSE 20000                                   ' warm-up/inter-show delay
  timer = 0                                     ' clear for PIR debounce

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

Main:
   timer = 0                                     ' reset timer

Check_Trigger:
  ' Debounce PIR to keep erratic behaviour
   PAUSE 5
   timer = timer + 5 * Trigger                   ' advance/clear timer
   DEBUG CLS
   DEBUG "Debouncing the IR trigger"
   DEBUG timer
   DEBUG Trigger
   IF timer < 10 THEN Check_Trigger              ' wait for valid signal

   'Wait for Victim
   DEBUG CLS
   DEBUG "Waiting for Victim"
   DEBUG trigger
   IF Trigger = No THEN Main                     ' Wait for the next "victim"

   'Start Scaring the Victim
   DEBUG "Start scaring the Victim"
   RANDOM lottery                                   ' stir random #
   jumpMax = lottery // 3 + 2                       ' 2 to 5 jumps
   DEBUG jumps


Jump_Baby:
'Body jumping
  FOR jumps = 1 TO JumpMax
    DEBUG jumps
    BodyJump = IsOn

     'Head Shaking
       RANDOM lottery                              ' stir random #
       twitchMax = lottery // 3 + 5                 ' 3 to 8 twitches
       DEBUG twitch
       FOR twitch = 1 TO twitchMax
         DEBUG twitch
         HIGH HeadShake                          ' pulse cylinder On
         DEBUG HeadShake
         PAUSE 500
         LOW HeadShake                         ' pulse cylinder Off
         DEBUG HeadShake
       NEXT

    BodyJump = IsOff
    BodyJump = IsOn
    BodyJump = IsOff

     'Head Shaking
       RANDOM lottery                              ' stir random #
       twitchMax = lottery // 3 + 5                   ' 3 to 8 twitches
       DEBUG twitch
       FOR twitch = 1 TO twitchMax
         DEBUG twitch
         HIGH HeadShake                          ' pulse cylinder On
         PAUSE 500
         LOW HeadShake                         ' pulse cylinder Off
       NEXT

  NEXT

Rest:
  BodyJump = IsOff
  DEBUG "Pausing..."
  PAUSE 30000                                   ' hold 30 secs
  GOTO Main                                     ' start over


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


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


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

JonnyMac

Oi!  You're mixing modes making a mess!  ;D

No worries, I can help you -- but... it will be easier if you just describe what you want (in English, not code) and let me write the program for you.   You're close, but making some subtle errors that bite hard if you don't watch for them.
Jon McPhalen
EFX-TEK Hollywood Office

JonnyMac

Okay, I want to get out this evening so I played mind reader and am making this guess -- see if this gets closer to what you want.

Hints:
1) You have to set the output pins using DIRS -- you forgot about P1
2) You CANNOT use HIGH and LOW with pins defined as PIN0, PIN1, etc. -- you must assign make them outputs and assign a value

' =========================================================================
'
'   File...... Chair Torture.BS1
'   Purpose...
'   Author.... Chris Villanueva (updates by JW)
'   E-mail.... Chris@Vspot.net
'   Started... 2009-09-26
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================

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


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


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

SYMBOL  Trigger         = PIN6                  ' SETUP = DN
SYMBOL  BodyJump        = PIN1                  ' use V+/OUT1
SYMBOL  HeadShake       = PIN0                  ' use V+/OUT0


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

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

SYMBOL  Yes             = 1
SYMBOL  No              = 0


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

SYMBOL  jumps           = B2
SYMBOL  twitches        = B3

SYMBOL  timer           = W4
SYMBOL  lottery         = W5


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

Reset:
  PINS = %00000000                              ' clear all
  DIRS = %00000011                              ' set outputs -- P0 & P1

  PAUSE 20000                                   ' warm-up/inter-show delay


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

Main:
  timer = 0                                     ' reset timer

Check_Trigger:
  RANDOM lottery                                ' stir random #
  PAUSE 5
  timer = timer + 5 * Trigger                   ' advance/clear timer
  IF timer < 100 THEN Check_Trigger             ' wait for valid signal

  jumps = lottery // 4 + 2                      ' 2 to 5 jumps

Jump_Baby:
  BodyJump = IsOn
  RANDOM lottery
  twitches = lottery // 11 + 6                  ' 3 (6) to 8 (16) twitches
  twitches = twitches / 2                       ' make even #

Get_Twitchy:
  HeadShake = IsOn - HeadShake                  ' twitch other way
  RANDOM lottery
  timer = lottery // 201 + 200                  ' 200 to 400
  PAUSE timer
  twitches = twitches - 1
  IF twitches > 0 THEN Get_Twitchy

  HeadShake = IsOff
  BodyJump = IsOff                              ' rest a bit
  RANDOM lottery
  timer = lottery // 501 + 500                  ' 500 to 1000
  PAUSE timer

  jumps = jumps - 1
  IF jumps > 0 THEN Jump_Baby                   ' right back at'cha

  GOTO Reset


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


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


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

greendonkey

Wow... It sounds like I really need to go back to PBASIC class!! I'm completely ignorant of the "mixed modes" factor!! I need to figure out where I can refresh my memory on that one!!

Thanks for reading my mind and putting it down in code for me!! I knew it had to be something basic that I just wasn't getting...

Say, l had a question... last year all your code had a bunch of debouncing code that seemed to make a lot of sense. In this example, there isn't any of that code. Is this a change in your thought process on the subject.

Thanks SO much!!
Chris

JonnyMac

September 27, 2009, 12:22:57 PM #4 Last Edit: September 27, 2009, 12:26:37 PM by JonnyMac
Had it (the debouncing code) been a snake it would have bit you!  This is it -- and it is in all my programs except those that need to run in a constant loop.

Main:
 timer = 0                                     ' reset timer

Check_Trigger:
 RANDOM lottery                                ' stir random #
 PAUSE 5
 timer = timer + 5 * Trigger                   ' advance/clear timer
 IF timer < 100 THEN Check_Trigger             ' wait for valid signal


Since Trigger is an input that can be 0 (no signal) or 1 (has signal) it either advances (1) or clears (0) the variable called timer in this loop.  When Trigger is [continuously] active for at least 100ms the IF..THEN test will fail the program drops through to the operational code.
Jon McPhalen
EFX-TEK Hollywood Office