November 23, 2024, 09:24:51 AM

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.


Blinking Eyes - conversion from Prop 1

Started by Jeff Haas, September 29, 2009, 01:22:43 AM

Previous topic - Next topic

Jeff Haas

I've been trying to convert the Blinking Eyes code in this thread:
http://www.efx-tek.com/php/smf/index.php?topic=1177.0

to the Prop 2.  I've got it to compile and download, and all the LEDs on the Prop-1 Trainer come on, but then nothing blinks.  What have I missed?

I've added comments to the code (in CAPS) where I think changes should be made, but see the original thread for davisgraveyard's logic.  Also, this starts with all the LEDs on, once I understand where I've made the mistake, I'd like to change it so they all start off and then blink one at a time.

Quote' =========================================================================
'
'   File...... JD_Blinker.BS2
'   Purpose...Simulate human eye blinking
'   Author.... Jon Williams, EFX-TEK
'              Copyright (c) 2009 EFX-TEK
'              Some Rights Reserved
'              -- see http://creativecommons.org/licenses/by/3.0/
'
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated... 07 SEP 2009
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------
' Blinks LEDs like a human eye.

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

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


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

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

Yes          CON 1
No           CON 0


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

idx             VAR B0                    ' loop controller
eye             VAR B1                    ' active "eye"
last            VAR B2                    ' last eye blinked
mask            VAR B3                    ' bit mask for eye
playlist        VAR B4                    ' eyes that have blinked
result          VAR B5
blinks          VAR B6                    ' #blinks
speed           VAR B7                    ' speed of blink
level           VAR B8                    ' for PWM

delay           VAR W5
lottery         VAR W6                    ' W6 okay with NO GOSUBs


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

Reset:
  OUTH = %00111111            ' all eyes on
  DIRH = %00111111            ' define output pins

  lottery = 1031

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

Main:
  FOR idx = 1 TO 3                              ' big stir
    RANDOM lottery
  NEXT

Select_Eye:
  RANDOM lottery
  eye = lottery // 6                            ' select P0..P5
  IF eye = last THEN Select_Eye                 ' don't repeat eye
    last = eye                                  ' save for next cycle

  READ eye, mask                                ' get bitmask for eye
  result = playlist & mask                      ' check playlist
  IF result > 0 THEN Select_Eye                 ' try again if run this cycle
    playlist = playlist | mask                  ' mark
    IF playlist <> %00111111 THEN Set_Blinks
      playlist = %00000000

Set_Blinks:
  RANDOM lottery
  blinks = lottery // 2 + 1                     ' 1 or 2 "blinks"

Set_Speed:
  RANDOM lottery
  speed = lottery // 10 + 5                     ' set blink speed

Blink_Baby:
  FOR level = 0 TO 255 STEP speed               ' open eye
    PWM eye, level, 1
'     PWM eye, 170, 175                         ' ALT CODE FOR BLINKING, INCL BS2 VALUE
'     PWM eye, 0 , 175
'   PAUSE 3000                                  ' ADDED PAUSE
  NEXT

  FOR level = 255 TO 0 STEP -speed              ' close eye
  PWM eye, level, 1
'  PWM eye, 170, 175                            ' ALT CODE FOR BLINKING
'  PWM eye, 0 , 175
  NEXT
  HIGH eye
  blinks = blinks - 1
  IF blinks > 0 THEN Blink_Baby

  RANDOM lottery
  delay = lottery // 1001 + 1000                ' 1s to 2s delay
  PAUSE delay

  GOTO Main


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

Bit_Masks:
  DATA  %00000001
  DATA  %00000010
  DATA  %00000100
  DATA  %00001000
  DATA  %00010000
  DATA  %00100000

JonnyMac

You went astray in a couple places:

1) Variable definitions.  NEVER use internal variable names (B0, B1, W1, etc.) with the Prop-2; define variables by type (Byte or Word).

2) You setup the outputs for P8-P15 but the body of the code wants to run on P0-P7.

Here's my version.  Note that the only change in the body is the PWM cycles from 1 (Prop-1) to 5 (Prop-2) -- this is needed because the Prop-2 is 5x as fast as the Prop-1.

' =========================================================================
'
'   File...... JD_Blinker.BS2
'   Purpose...Simulate human eye blinking
'   Author.... Jon Williams, EFX-TEK
'              Copyright (c) 2009 EFX-TEK
'              Some Rights Reserved
'              -- see http://creativecommons.org/licenses/by/3.0/
'
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated... 07 SEP 2009
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------
' Blinks LEDs like a human eye.

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

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


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

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

Yes             CON     1
No              CON     0


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

idx             VAR     Byte                    ' loop controller
eye             VAR     Byte                    ' active "eye"
last            VAR     Byte                    ' last eye blinked
mask            VAR     Byte                    ' bit mask for eye
playlist        VAR     Byte                    ' eyes that have blinked
result          VAR     Byte
blinks          VAR     Byte                    ' #blinks
speed           VAR     Byte                    ' speed of blink
level           VAR     Byte                    ' for PWM

delay           VAR     Word
lottery         VAR     Word


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

Reset:
 OUTH = %00000000 : OUTL = %00111111           ' all eyes on
 DIRH = %00000000 : DIRL = %00111111           ' define output pins

 lottery = 1031


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

Main:
 FOR idx = 1 TO 3                              ' big stir
   RANDOM lottery
 NEXT

Select_Eye:
 RANDOM lottery
 eye = lottery // 6                            ' select P0..P5
 IF eye = last THEN Select_Eye                 ' don't repeat eye
   last = eye                                  ' save for next cycle

 READ eye, mask                                ' get bitmask for eye
 result = playlist & mask                      ' check playlist
 IF result > 0 THEN Select_Eye                 ' try again if run this cycle
   playlist = playlist | mask                  ' mark
   IF playlist <> %00111111 THEN Set_Blinks
     playlist = %00000000

Set_Blinks:
 RANDOM lottery
 blinks = lottery // 2 + 1                     ' 1 or 2 "blinks"

Set_Speed:
 RANDOM lottery
 speed = lottery // 10 + 5                     ' set blink speed

Blink_Baby:
 FOR level = 255 TO 0 STEP -speed              ' close eye
   PWM eye, level, 5
 NEXT
 FOR level = 0 TO 255 STEP speed               ' open eye
   PWM eye, level, 5
 NEXT
 HIGH eye
 blinks = blinks - 1
 IF blinks > 0 THEN Blink_Baby

 RANDOM lottery
 delay = lottery // 1001 + 1000                ' 1s to 2s delay
 PAUSE delay

 GOTO Main



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

Bit_Masks       DATA  %00000001
               DATA  %00000010
               DATA  %00000100
               DATA  %00001000
               DATA  %00010000
               DATA  %00100000
Jon McPhalen
EFX-TEK Hollywood Office

Jeff Haas

Thanks for catching my mistakes!  I'll get to hooking this up on the weekend.