Shop OBEX P1 Docs P2 Docs Learn Events
How can I fade an LED on or off?? — Parallax Forums

How can I fade an LED on or off??

wolfdaddy33wolfdaddy33 Posts: 12
edited 2012-03-21 13:15 in BASIC Stamp
I'm a newbie to Basic Stamp. I am using Stamp BS2 and PBASIC 2.5.

I have a project which needs to fade an LED ON or OFF, and would like to dictate the speed of the fade.

Does anyone have code to perform this or a tutorial I can be directed to??

Thanks.

Comments

  • Mike2545Mike2545 Posts: 433
    edited 2012-03-12 08:12
    Check out the PWM in the manual...
    You can basically just give the led a duty cycle from 100% to 0% in steps down with a 'for/next' loop.
  • ercoerco Posts: 20,256
    edited 2012-03-12 14:51
    Yep, use the PWM command or even better, you can write your own code to vary the duty cycle of the LED.

    Go, wolfdaddy33! It's yer birthday! Uh-huh!
  • wolfdaddy33wolfdaddy33 Posts: 12
    edited 2012-03-12 18:07
    I will give it a shot. Thank you for your responses. Its been a lot of fun learning.

    I Will post updates, cause I'm sure to screw something up.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2012-03-12 19:52
  • RiJoRiRiJoRi Posts: 157
    edited 2012-03-14 13:52
    OK, it's been a while, but I did something like this (PWM-ing an LED) with a Propeller. Doing a straight PWM (10/90, 20/80, ...,90/10) did not look right. The human eye has logarithmic, not linear, sensitivity. Sorry, but I do not remember how I diddled the numbers!

    --Rich
  • RDL2004RDL2004 Posts: 2,554
    edited 2012-03-14 14:28
    Experiment with this on your Basic Stamp.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    'Variables ------------------------------------------------------------------------
    
    dty VAR Byte
    
    ' Syntax is:
    ' PWM PIN, duty(cycle), duration
    '
    ' In this program "duty" controls the brightness range (0 to 255),
    ' duration controls the speed of the pulsing (1 to 20 works best-higher is slower).
    ' Connect the LED between pin 11 and ground (Vss)and use a current limit resistor.
    '----------------------------------------------------------------------------------
    
    'First, ramp the brightness up. The low value of 20 keeps LED from going
    'totally dark. Experiment with STEP values in the 1-20 range.
    'Note that ramp up and ramp down do not have to be the same.
    
    DO
    FOR dty = 20 TO 255 STEP 10
    
    PWM 11, dty, 1
    
    NEXT
    
    'Now ramp the brightness down. You can add a Step value here too.
    
    FOR dty = 255 TO 20
    
    PWM 11, dty, 5
    
    NEXT
    
    LOOP
    
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2012-03-14 19:41
    You can get power law changes with code something like the following, which multiplies (instead of adds) the current intensity times a factor at each step. The factor is less than unity for decreasing and greater than unity for increasing. The larger the rateFactor, the faster the fade.
    [SIZE=1][FONT=arial narrow] rateFactor CON 64      ' larger value for larger step.  64 is x-> x^0.75 or x^1.25
        fadeFactor CON 256 - rateFactor    ' less than unity
        growFactor CON 256 + rateFactor   ' greater than unity
       growThreshold CON 255   ' maximum for PWM
       fadeThreshold CON 10    ' choose depending on ambient light.
       x VAR Word
        DO
            DO     ' increase level in steps
               PWM 0,x,100
               x = x */ growFactor
            LOOP UNTIL x>growThreshold
           DO       ' decrease level in steps
              PWM 0, x  ,100
              x = x */ fadeFactor
            LOOP UNTIL x< fadeThreshold
         LOOP[/FONT][/SIZE]
    

    The perception of intensity depends a lot on ambient light level, so the limit of the fade may need to be adjusted to account for that.

    This uses the Stamp's */ operator, which is the the Stamp's way to multiply times fractions. Unity=256. By that, I mean that x = x */ 256 makes no change in the value of x, but x = x */ 192 is like multiplying x times 192/256 = 3/4.

    You can also get a pulsing effect with the FREQOUT command
    FREQOUT 0,1000,1000,1001
    That gives nice sinusoidal fade in and fade out due to the beat frequency of 1000 Hz with 1001 Hz .
  • wolfdaddy33wolfdaddy33 Posts: 12
    edited 2012-03-15 08:57
    Excellent Post!! I've been experimenting with this all morning. I went with a step of 1 to try for a more natural fade, but will see if I get a better result with Factor rate posted by Tracy Allen.
  • wolfdaddy33wolfdaddy33 Posts: 12
    edited 2012-03-20 18:14
    I must be doing something wrong. I tried the FREQOUT command but could not get LED to fade please forgive my ignorance I have tried different values with no result.

    Example....

    BS2
    Basic stamp 2.5

    Warninglight:
    FREQOUT 0, 1000, 1000, 1001

    goto Warninglight
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2012-03-20 21:41
    No, it's not you. I think I was remembering something else, having to do with playing the beat frequency over a speaker. With the LED try this,
    DO
      FREQOUT 0,5000,1
    LOOP
    
    or beat frequencies
    DO
      FREQOUT 0,5000,1,3
    LOOP
    
    or freaky candle effect
    DO
    RANDOM W0
    FREQOUT 0,1000,B0//3,B1//5
    LOOP
    
  • wolfdaddy33wolfdaddy33 Posts: 12
    edited 2012-03-21 07:06
    Works AWESOME!! really like the candle effect. Do you know how I can perform a really, really slow linear looking fade?
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2012-03-21 08:47
    Here is an example. This uses the power law idea. When I posted this above, it should have been using the upper byte of the variable--I forgot that the PWM parameter is a byte, and the lowByte would be changing too fast.
    [SIZE=1][FONT=courier new]' {$STAMP BS2}
    ' {$PBASIC 2.5}
    fadeFactor CON 64000      '<--- if closer to 65535, slower fade; lower number, faster fade
    fadeThreshold CON 3000    '<--- experiment, depends on LED and on ambient light.
    x VAR WORD
    x1 VAR x.BYTE1
    DO
      HIGH 0       ' start LED bright
      PAUSE 1000
      x=65535      ' fade starts at high intensity
      DO       ' decrease level in power law steps
        PWM 0, x1  ,100
        x = x ** fadeFactor
      LOOP UNTIL x < fadeThreshold
    LOOP[/FONT][/SIZE]
    
  • wolfdaddy33wolfdaddy33 Posts: 12
    edited 2012-03-21 09:05
    That works great and it gives such a linear motion! You would think someone was fading it with a potentiometer, I don't think I could even fade that slowly,and evenly with a pot.

    Very Impressive.
  • wolfdaddy33wolfdaddy33 Posts: 12
    edited 2012-03-21 09:09
    Is it possible to use the same type of code to move a servo slowly and evenly back and forth??
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2012-03-21 10:24
    This is more what I meant by a fade that accelerates as the light becomes dimmer:
    [SIZE=1][FONT=courier new]' {$STAMP BS2}
    ' {$PBASIC 2.5}
    fadeFactor CON 65500 '<--- If closer TO 65535=slower fade; lower number=faster
    fadeThreshold CON 2000' <--- experiment, depends on LED and on ambient light.
    fadeAcceleration CON 50  ' <--- experiment, fade accelerates at low end
    f VAR WORD
    x VAR WORD
    x1 VAR x.BYTE1
    DO
     HIGH 0 ' start LED bright
     PAUSE 1000
     x=65535 ' fade starts at high intensity
     f=fadefactor
     DO ' decrease level in power law steps
       PWM 0, x1 ,100
       x = x ** f  
       f = f - fadeAcceleration
     LOOP UNTIL x < fadeThreshold
    LOOP[/FONT][/SIZE]
    

    The same general idea ought to work for a servo, with either linear or decelerated movements. For servos, the program is periodically applying pulses between 1 and 2 milliseconds, not PWM. Quite different in that respect.
  • wolfdaddy33wolfdaddy33 Posts: 12
    edited 2012-03-21 13:15
    Ok this works very well for fading off. I'm a bit confused on what x.BYTE1 is??
    I know that Byte is 0-255 but what are you telling the stamp for x.BYTE1 variable?

    Also I tried to go the other way to fade the light ON but was not very successful.
    How would you fade ON to increase the same way?
Sign In or Register to comment.