Haunt Forum banner

1 - 6 of 6 Posts

·
Premium Member
Joined
·
5,858 Posts
Discussion Starter #1
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:

Code:
' {$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?
 

·
Premium Member
Joined
·
5,858 Posts
Discussion Starter #3
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.
 

·
Registered
Joined
·
831 Posts
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

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]
 

·
Registered
Joined
·
432 Posts
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. :)
 

·
Premium Member
Joined
·
5,858 Posts
Discussion Starter #6
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:

PWM PIN0, level, cycles

PIN0 is actually a variable that holds the present level of pin 0, so in the beginning -- when the pin is low -- its value is zero and your PWM function is:

PWM 0, level, cycles

... so P0 appropriately fades up. But then you make P0 HIGH, which writes a 1 to the P0 output register, and this register can be read back using the PIN0 variable. So, you're fade down section worked out to:

PWM 1, level, cycles

You see, the program was trying to run PWM on a pin without an LED. The lesson is this: You only need to define a pin alias with the PINx notation when you want to directly read from or write to the pin in your program, for example:

IF Trigger = IsOff THEN Main ' reading from pin; Trigger is alias for PIN6
PopUp = IsOn ' writing to pin; PopUp is alias for PIN5

When PINx is used to the right of anything, as in the Trigger line (where it is to the right of IF), then the program will read from PINx and substitute the present value (0 [low] or 1 [high]) into the code. When the very first thing on a line is PINx =, as in the PopUp line, the program will write the the Px output register; if the pin is in output mode then the value, 0 or 1, will show up on the physical output.

Sorry, gang, I had a late night last night and this has taken me a few edits to explain -- I hope it makes sense
 
1 - 6 of 6 Posts
Top