November 22, 2024, 02:48:07 PM

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.


LED fader

Started by imagineerdan, October 26, 2007, 03:45:07 PM

Previous topic - Next topic

imagineerdan

I am trying to make a sequince of leds pwm on then of in a sequincial manner from pin 1 through7. basically a fading running light. What would this look like?

JonnyMac

Is this what you're looking for?  It will probably take some tweaking of the PWM loops to get it just the way you want.

' =========================================================================
'
'   File...... Running_Light.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'              Copyright (c) 2007 EFX-TEK
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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


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


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

SYMBOL  pinNum          = B2
SYMBOL  level           = B3


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

Reset:


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

Main:
  FOR pinNum = 0 TO 7                           ' loop through outputs
    FOR level = 255 TO 0 STEP -8                ' bright to dark
      PWM pinNum, level, 2
    NEXT
  NEXT
  GOTO Main


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


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


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

imagineerdan

Im also using this program, I would like to make it run faster and also utilize ALL the pins how would I alter it?. Thanks

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

SYMBOL  Lamp3           = PIN3
SYMBOL  Lamp2           = PIN2
SYMBOL  Lamp1           = PIN1
SYMBOL  Lamp0           = PIN0


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


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

SYMBOL  theLamp         = B2                    ' lamp to brighten or dim
SYMBOL  last            = B3                    ' last lamp adjusted
SYMBOL  level           = B4                    ' output level (for PWM)
SYMBOL  mask            = B5                    ' bit mask test
SYMBOL  temp            = B6
SYMBOL  speed           = B7                    ' adjustment speed
SYMBOL  lottery         = W5                    ' random value


' -----[ EEPROM Data ]-----------------------------------------------------

Bit_Set:
  EEPROM (%0001, %0010, %0100, %1000)


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

Reset:
  DIRS = %00001111                              ' P0 - P3 are outs
  lottery = 1031                                ' seed random value


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

Main:
  RANDOM lottery                                ' tumble random value
  theLamp = lottery // 4                        ' select a lamp, 0 - 3
  IF theLamp = last THEN Main                   ' no repeats
    last = theLamp

  READ theLamp, mask                            ' get bit for that lamp
  temp = PINS & mask                            ' test selected lamp
  IF temp = 0 THEN Brighten

Dim:
  RANDOM lottery
  speed = lottery // 6 + 5                      ' set speed, 5 to 10
  FOR level = 255 TO 0 STEP -speed              ' decrease output level
    PWM theLamp, level, 1
  NEXT
  LOW theLamp                                   ' lamp fully off
  GOTO Main

Brighten:
  RANDOM lottery
  speed = lottery // 3 + 3                      ' set speed 3 to 5
  FOR level = 0 TO 255 STEP speed               ' increase output level
    PWM theLamp, level, 1
  NEXT
  HIGH theLamp                                  ' lamp fully on
  GOTO Main


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

JonnyMac

Updating for all outputs is easy -- but I'm not sure what "faster" is to you.  I have modified it so that the fade and dim loops run faster than in your original, perhaps this will be enough.

' {$STAMP BS1}

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

SYMBOL  Lamp7           = PIN7
SYMBOL  Lamp6           = PIN6
SYMBOL  Lamp5           = PIN5
SYMBOL  Lamp4           = PIN4
SYMBOL  Lamp3           = PIN3
SYMBOL  Lamp2           = PIN2
SYMBOL  Lamp1           = PIN1
SYMBOL  Lamp0           = PIN0


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


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

SYMBOL  theLamp         = B2                    ' lamp to brighten or dim
SYMBOL  last            = B3                    ' last lamp adjusted
SYMBOL  level           = B4                    ' output level (for PWM)
SYMBOL  mask            = B5                    ' bit mask test
SYMBOL  temp            = B6
SYMBOL  speed           = B7                    ' adjustment speed
SYMBOL  lottery         = W5                    ' random value


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

Reset:
  DIRS = %11111111                              ' P0 - P7 are outs
  lottery = 1031                                ' seed random value


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

Main:
  RANDOM lottery                                ' tumble random value
  theLamp = lottery // 8                        ' select a lamp, 0 - 8
  IF theLamp = last THEN Main                   ' no repeats
    last = theLamp

  READ theLamp, mask                            ' get bit for that lamp
  temp = PINS & mask                            ' test selected lamp
  IF temp = 0 THEN Brighten

Dim:
  RANDOM lottery
  speed = lottery // 11 + 10                    ' set speed, 10 to 20
  FOR level = 255 TO 0 STEP -speed              ' decrease output level
    PWM theLamp, level, 1
  NEXT
  LOW theLamp                                   ' lamp fully off
  GOTO Main

Brighten:
  RANDOM lottery
  speed = lottery // 11 + 10                    ' set speed, 10 to 20
  FOR level = 0 TO 255 STEP speed               ' increase output level
    PWM theLamp, level, 1
  NEXT
  HIGH theLamp                                  ' lamp fully on
  GOTO Main


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


' -----[ EEPROM Data ]-----------------------------------------------------

Bit_Set:
  EEPROM (%00000001, %00000010, %00000100, %00001000)
  EEPROM (%00010000, %00100000, %01000000, %10000000)
Jon McPhalen
EFX-TEK Hollywood Office