How can I fade an LED on or off??
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.
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
You can basically just give the led a duty cycle from 100% to 0% in steps down with a 'for/next' loop.
Go, wolfdaddy33! It's yer birthday! Uh-huh!
I Will post updates, cause I'm sure to screw something up.
http://www.youtube.com/watch?v=OUpNYQgWRtM
--Rich
' {$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[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 .
Example....
BS2
Basic stamp 2.5
Warninglight:
FREQOUT 0, 1000, 1000, 1001
goto Warninglight
[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]Very Impressive.
[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.
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?