# Problem Fading LEDs



## Zombie-F (Apr 12, 2004)

Ok, so I'm trying to use the basic stamp PWM command to fade on a pair of LED eyes, hold it on for 8 seconds, fade them down and hold them off for 8 seconds. Here is the code I have come up with:


```
' {$STAMP BS1}
' {$PBASIC 1.0}
' =========================================================================
'
' © 2006 Unpleasant Street
' File....... LEDFader.bs1
' Purpose.... Fade an LED on and off
' Author..... David Lindblom
' E-mail..... [email protected]
'
' =========================================================================

' -----[ Program Description ]---------------------------------------------
'
' This is a PROP-1 program designed to fade an LED on and off using
' persistance of vision to trick the eye into thinking the LED is fading
' on and then off.

' -----[ Revision History ]------------------------------------------------
'
' LEDFader.bs1 - January 29, 2007 - First version.

' -----[ I/O Definitions ]-------------------------------------------------
'
SYMBOL  eyes            = PIN0     ' Pin assigned to Gargoyle's eyes

' -----[ Constants ]-------------------------------------------------------
'
SYMBOL  No              = 0
SYMBOL  Yes             = 1

' -----[ Variables ]-------------------------------------------------------
'
SYMBOL  level           = B1       ' Brightness level

' -----[ Initialization ]--------------------------------------------------
'
Reset:

  '       76543210        ' bit positions
  DIRS = %11111111        ' make P7 - P0 outputs
  PINS = %00000000        ' P7 - P0 off

' -----[ Program Code ]----------------------------------------------------
'
' We start each cycle up here.
' We will come here after every run of the program.

Main:

' Fade eyes on
  FOR level = 0 TO 250 STEP 5
    PWM eyes, level, 8
  NEXT

' Eyes on
  HIGH eyes
  PAUSE 8000
  DEBUG "What is going on here?"

' Fade eyes off
  FOR level = 255 TO 0 STEP -5
    PWM eyes, level, 8
  NEXT

' Eyes off
  LOW eyes
  PAUSE 8000
  DEBUG "I have no idea what is going on."

' Back to the top
  GOTO Main
```
The snag I've hit is that the LEDs fade on, but then never fade off. Through the use of the debug lines in there, I can tell the program is executing beyond the "HIGH eyes" command, but for some reason the eyes are not fading out.

Any ideas?


----------



## Dr Morbius (Sep 21, 2004)

The only wierdness I see, and believe me, I have no idea how to program a basic stamp, but I DID notice you have a value of 250 in your FOR/NEXT statement in the fade "ON" line and a value of 255 in the fade "OFF" line. Did you do that on purpose?


----------



## Zombie-F (Apr 12, 2004)

Dr Morbius said:


> The only wierdness I see, and believe me, I have no idea how to program a basic stamp, but I DID notice you have a value of 250 in your FOR/NEXT statement in the fade "ON" line and a value of 255 in the fade "OFF" line. Did you do that on purpose?


Yes. To fade on, you want it to fade from the lowest (0) to just below the highest setting (255 being the highest) and have the "HIGH eyes" command tell the stamp to go full brightness on that output pin.

To fade off you must take it down from max to min.


----------



## krough (Oct 7, 2004)

Here is what we did for my fading bat eyes in the cage last year.

its a bit different. it was originally from Parallax, and we altered the on-off times at the end of the code


```
' Krough Changes I made were mainly towards randomizing the on and off times.

' =========================================================================
'
'   File....... Spooky_Eyes.BS1
'   Purpose.... Random "eyes" controller for Halloween haunts
'   Author..... Jon Williams -- Parallax, Inc.
'   E-mail..... [email][email protected][/email]
'   Started....
'   Updated.... 16 JAN 2005
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------
'
' While triggered, this program will randomly select an set of "eyes" and
' bring them on gradually, hold them on for a moment, then extinguish
' them.  The process will complete as long as the trigger is present.


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

' 15 JAN 2005 - First version; tested on Parallax BS1 Project Board


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

SYMBOL  Trigger         = PIN7


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

SYMBOL  IsOn            = 0                     ' active low input
SYMBOL  IsOff           = 1

SYMBOL  EyePorts        = 4                     ' # eye control pins


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

SYMBOL  randVal         = W1                    ' for random # generator
SYMBOL  eyeNum          = B4                    ' selected eye pin
SYMBOL  lastNum         = B5                    ' last eye on
SYMBOL  level           = B6                    ' brighness level
SYMBOL  pauseTime       = W4                    ' pause Time


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

Reset:
  lastNum = EyePorts - 1                        ' calc last control pin
  FOR eyeNum = 0 TO lastNum                     ' set used control pins
    LOW eyeNum                                  ' make output and off
  NEXT

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

Main:
  RANDOM randVal                                ' tumble random generator
  'IF Trigger = IsOn THEN Main                  ' wait for trigger

Get_Eye:
  eyeNum = randVal // EyePorts                  ' make 0 to (EyePorts - 1)
  IF eyeNum = lastNum THEN Main                 ' do not allow repeat
  lastNum = eyeNum                              ' save current selection

Show_Eye:
  FOR level = 0 TO 255 STEP 1                   ' increase brightness
    PWM eyeNum, level, 3                        ' modulate LED eyes
  NEXT
  HIGH eyeNum                                   ' full bright
  RANDOM randVal                                ' tumble random generator
  pauseTime = randVal / 8 + 2000                ' pause time between 2 and 10 seconds
  PAUSE pauseTime                               ' leave on for a bit
  FOR level = 255 TO 0 STEP -1                  ' decrease brightness
    PWM eyeNum, level, 3                        ' modeulate LED eyes
  NEXT
  LOW eyeNum                                    ' selected eye off
  RANDOM randVal                                ' tumble random generator
  pauseTime = randVal / 8 + 2000                ' pause time between 2 and 10 seconds
  PAUSE pauseTime                               ' inter-eye delay
  GOTO Main[code]
```
[/CODE]


----------



## NecroBones (Feb 10, 2006)

I haven't worked with the hardware you're using, but is it intended for LEDs or for incandescent? LEDs don't fade the same way in terms of regulating the voltage, and instead need to be flickered rapidly with different durations for the pulses...

Then again, looking at your message, they're fading ON ok, but not fading OFF. Hooking up an LED to a dimmer would get you the opposite, since they have a minimum starting voltage, and can be dimmed back only after being powered up.

Anyway, probably not helpful. Just trying to understand the problem.


----------



## Zombie-F (Apr 12, 2004)

EFX-TEK answered my question for me. Here's what JohnnyMac had to say:



johnnymac said:


> Cool. In case you're wondering, here's what happened when using PIN0 instead of 0:
> 
> Removing the pin alias, your PWM code worked out to:
> 
> ...


----------

