LED random fade
haunteddansion
Posts: 68
I am trying to get an LED to PWM and ramp up and down at random speeds over and over again, How would this code look. Thanks for the help!
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
' {$PBASIC 2.5}
Ramp· VAR Byte
speed VAR Byte
x···· VAR Byte
result VAR Word
DO
RANDOM result
RAMP=RESULT.HIGHBYTE MIN 64
RANDOM result
speed=RESULT.HIGHNIB· MIN 1
FOR x=32 TO ramp STEP speed
· PWM 0,x,5
NEXT
RANDOM result
speed=RESULT.HIGHNIB· MIN 1
FOR x=ramp TO 32 STEP speed
· PWM 0,x,1
NEXT
Loop
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun
TR
If you need more current (brighter LED), decrease the values of both resistors proportionately and increase the cap value.
If you need to do this to a whole string of LEDs, you can connect them in series and apply a higher voltage to the anode end of the string. They will all draw the same current, based on the value of the emitter resistor. Just be sure that the voltage between the transistor's emitter and collector is kept low, so the transistor doesn't overheat — or worse.
Here's a sample program that works on the BS2 for randomly flickering the LED's brightness. You should be able to adapt it for the BS1 and build from there.
Happy haunting!
-Phil
It might be more interesting to bias the statistics toward longer runs. Light level perception is somewhat nonlinear, and is perceived in relation to ambient light, so PWM output would better take that into account by enforcing a minimum limit for duty and by taking larger steps within higher brightness levels.
I think you can hook the LED and series resistor directly to the Stamp pin to control the brightness with PWM.
PWM
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
-Phil
P.S. to Tracy: Watch out for over/underflow with your random walk.
I re-read your post your looking for this:
'LED connected in series to Pin 0 with 470 ohm resistor
' {$STAMP BS2}
' {$PBASIC 2.5}
Ramp· VAR Byte
speed VAR Byte
x···· VAR Byte
result VAR Word
DO
RANDOM result
speed=RESULT // 16 MIN 1
FOR x=255 TO 0 STEP speed
· PWM 0,x,300
NEXT
PAUSE 100
FOR x=0 TO 255 STEP speed
· PWM 0,x,300
NEXT
PWM 0,255,1000/speed
LOOP
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun
TR
Post Edited (TechnoRobbo) : 9/20/2007 2:30:46 AM GMT
Here is a variation on TR's approach (also on a BS2). Instead of a linear increase and decrease, it uses a proportionality, successive multiplication by */ a factor near unity. The rationale is to sort of match the logarithmic transfer function of the eye. The visual impression is rather different.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
The values Tracy included above are the same, I just had to re-organize everything to run on my processor.
Each pixel has it's own set of random numbers which drive the up and down fade times.
The processor is actually calculating values for 170 pixels but only displaying 19.
The first blue/purple section of this video clip shows the results:
www.youtube.com/watch?v=FF1YUCV_JAw
Thanks for a nifty bit of code!
John
Post Edited (JohnC) : 9/27/2007 8:09:47 PM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
If you're not familiar with DMX, it's a data protocol used extensively in the theatre for controlling dimmers and moving lights.
The pixels each have a discrete address and only respond to their assigned portion of the data stream. The pixel also generates 3 channels of PWM at about 100 Hz.
I'm actually a bit proud of the code, as it was my first stab at writing anything in pure assembly.
Each pixel also has an RS-485 (differential mode) receiver. The pixels are daisy-chained with RJ45 (cat5) patch cables.
The DMX data signal is generated by a Pic 18F4550 which drives a matching RS-485 transmitter. It's fast enough that 512 bytes of data can be transmitted 44 times per second.
A group of people have been discussing the project on this forum:
www.christmasinshirley.com/forum/viewforum.php?f=33
There's more detail about my project, including schematics and source code, at
www.response-box.com/rgblights.shtml
Sorry it's not strictly a Basic Stamp project.
Yesterday when I was searching for flame emulation code, a friend pointed me to this thread. For Halloween I plan to line the roof of my house (above the garage) with pixels. Hopefully they'll be emulating blue / purple / amber flames.