November 02, 2024, 02:41:32 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.


8pin fading chase needs some direction

Started by clinefx1, September 02, 2008, 11:50:52 PM

Previous topic - Next topic

clinefx1

Hey Jon,

I've built an led strip light with 8 segments. It's purpose is to chase back and forth.  It's fits into the headlight of our golf cart at work and will kinda be a joke for the guys.  I have a simple program that will off/on back and forth and it's perfectly fine.  I've been working on a second program that will do a fading chase and look much nicer but I'm maxed out on memory.  I've tried for the past few days to implement some of your tricks but without any luck.  Would you help me skin another cat? No rush.  I've got 8 outputs, a fade up size and a fade down size.

Once again
Chris

menehune

September 03, 2008, 02:24:24 AM #1 Last Edit: September 03, 2008, 02:30:01 AM by menehune
Could you add some capacitors to the LED lights so the stamp pulses them on/off and the voltage slowly drops, ala the wickled?

Have you looked at the Fire Flies thread for PWM fade up/down?  I think the Prop1 can only perform PWM on one pin at a time though.

JonnyMac

September 03, 2008, 08:06:51 AM #2 Last Edit: September 03, 2008, 08:11:40 AM by JonnyMac
Chris,

This is a perfect example of where redundant code can be handled by subroutines.  Have a look at the listing below; it does what you want and uses only 1/4 of the Prop-1 program space.

Note the offset values going up versus going down; this is important so you get the proper "bounce" of the ends of the array.  Yeah, I've done of few of these things (can anyone say Cylon eyes?).

' =========================================================================
'
'   File...... Kitt_Kart.BS1
'   Purpose...
'   Author.... Chris Cline / Jon Williams
'   E-mail....
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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

SYMBOL  Leds            = PINS


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

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

SYMBOL  UpSpeed         = 20
SYMBOL  DnSpeed         = 7


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

SYMBOL  pinNum          = B2
SYMBOL  level           = B3


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

Reset:
  PINS = %00000000                              ' clear pins
  DIRS = %11111111                              ' make all outputs


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

Main:
  FOR pinNum = 0 TO 6
    GOSUB Fade_Up
    GOSUB Fade_Down
  NEXT
  FOR pinNum = 7 TO 1 STEP -1
    GOSUB Fade_Up
    GOSUB Fade_Down
  NEXT
  GOTO Main


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

Fade_Up:
  FOR level = 0 TO 255 STEP UpSpeed
    PWM pinNum, level, 1
  NEXT
  HIGH pinNum
  RETURN

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

Fade_Down:
  FOR level = 255 TO 0 STEP -DnSpeed
    PWM pinNum, level, 1
  NEXT
  LOW pinNum
  RETURN

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


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

clinefx1

Jon- Once again thanks

It seems so simple once the working code is on the page.  I wonder if you wouldn't mind taking this one step further.  If you look how I was laying the chase out in my huge program it leaped frogged between ups and downs generating a fading tail to the chase.  ie, p0- fup, p1-fup, p0-fdn, p2-fup, p1-fdn, p3-up etc.
How hard would it be too add this element?

Also, can a FOR / NEXT  statement have selected pins ie:  For pins =  1, 4, 6, 7 or For pinNum = 1 and 4 and 6?

-Chris

JonnyMac

Sorry, Chris, I saw one thing when you had clearly written another.  The good news is that PBASIC has some neat features, including a thing call LOOKUP that lets us map one value to another.  In this version of the program you can see how I use an index pointer with two LOOKUP tables; the first table is the pin to work with, the second table is its mode.  There are other ways to accomplish what you want but then involved encoding and decoding values -- when possible it's best to stick with the obvious.

' =========================================================================
'
'   File...... Kitt_Kart_v2.BS1
'   Purpose...
'   Author.... Chris Cline / Jon Williams
'   E-mail....
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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

SYMBOL  Leds            = PINS


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

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

SYMBOL  UpSpeed         = 20
SYMBOL  DnSpeed         = 7


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

SYMBOL  idx             = B2
SYMBOL  pntr            = B3
SYMBOL  pinNum          = B4
SYMBOL  mode            = B5
SYMBOL  level           = B6


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

Reset:
  PINS = %00000000                              ' clear pins
  DIRS = %11111111                              ' make all outputs


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

Main:
  FOR idx = 0 TO 15
    LOOKUP idx, (0,1,0,2,1,3,2,4,3,5,4,6,5,7,6,7), pinNum
    LOOKUP idx, (1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0), mode

Fwd_Up:
  IF mode <> 1 THEN Fwd_Down
    GOSUB Fade_Up
    GOTO Fwd_Next

Fwd_Down:
  GOSUB Fade_Down

Fwd_Next:
  NEXT


The_Turn:
  FOR idx = 0 TO 15
    LOOKUP idx, (7,6,7,5,6,4,5,3,4,2,3,1,2,0,1,0), pinNum
    LOOKUP idx, (1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0), mode

Rev_Up:
  IF mode <> 1 THEN Rev_Down
    GOSUB Fade_Up
    GOTO Rev_Next

Rev_Down:
  GOSUB Fade_Down

Rev_Next:
  NEXT


  GOTO Main


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

Fade_Up:
  FOR level = 0 TO 255 STEP UpSpeed
    PWM pinNum, level, 1
  NEXT
  HIGH pinNum
  RETURN

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

Fade_Down:
  FOR level = 255 TO 0 STEP -DnSpeed
    PWM pinNum, level, 1
  NEXT
  LOW pinNum
  RETURN

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


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

JonnyMac

Since part of my job is to teach you crazy cats how to write controller programs I've gone ahead and created a version of the program above that uses encoding/decoding.  Here's how it's done:

Since the pin number is less than 16 (0 to 7 in this case) we can fit that into a nibble which is one half of a byte; this allows us to use the other half of the byte for encoded information, in this case I am using one bit (BIT4) to hold the mode.  So for each step in the program there is a byte that might look like this: $17.  If we break that down the lower half (7) is the pin that will be affected; the upper half (1) is the mode (fade on).  Note that hex notation ($) is used to make the program easier to read/maintain.

' =========================================================================
'
'   File...... Kitt_Kart_v3.BS1
'   Purpose...
'   Author.... Chris Cline / Jon Williams
'   E-mail....
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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

SYMBOL  Leds            = PINS


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

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

SYMBOL  UpSpeed         = 20
SYMBOL  DnSpeed         = 7

SYMBOL  EOS             = $FF                   ' end of sequence


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

SYMBOL  pinNum          = B0
SYMBOL   mode           =  BIT4                 ' 1 = up, 0 = down

SYMBOL  pntr            = B2
SYMBOL  level           = B3


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

Reset:
  PINS = %00000000                              ' clear pins
  DIRS = %11111111                              ' make all outputs


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

Main:
  pntr = 0

Run_Sequence:
  READ pntr, pinNum
  IF pinNum = EOS THEN Main

Check_Mode:
  IF mode = 0 THEN Mode_Down

Mode_Up:
  pinNum = pinNum & $0F                         ' clear mode bit
  GOSUB Fade_Up
  pntr = pntr + 1
  GOTO Run_Sequence

Mode_Down:
  GOSUB Fade_Down
  pntr = pntr + 1
  GOTO Run_Sequence


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

Fade_Up:
  FOR level = 0 TO 255 STEP UpSpeed
    PWM pinNum, level, 1
  NEXT
  HIGH pinNum
  RETURN

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

Fade_Down:
  FOR level = 255 TO 0 STEP -DnSpeed
    PWM pinNum, level, 1
  NEXT
  LOW pinNum
  RETURN

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


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

Led_Table:
  EEPROM ($10, $11, $00, $12, $01, $13, $02, $14)
  EEPROM ($03, $15, $04, $16, $05, $17, $06, $07)
  EEPROM ($17, $16, $07, $15, $06, $14, $05, $13)
  EEPROM ($04, $12, $03, $11, $02, $10, $01, $00)
  EEPROM (EOS)
Jon McPhalen
EFX-TEK Hollywood Office

JonnyMac

Just a follow-up on my programming strategies.  If you look at a lot of my Prop-1 programs you'll see that I rarely use B0 and B1; there is a very specific reason for this: B0 and B1 are the only variables in the Prop-1 (BS1) that allow individual bit access.  By contrast in the Prop-2 (BS2) you can get to any bit of any variable.

So, in this case I wanted to use a bit to encode the mode of the pin for that table element.  By reading the table value into B0 (which is aliased as pinNum) we are able to look at the bit which has been assigned to BIT4.  Since the mode bit only directs where the program goes we have to strip it out before calling Fade_Up else that routine would be attempt to access a pin that doesn't exist.
Jon McPhalen
EFX-TEK Hollywood Office