May 18, 2024, 07:24:00 AM

News:

Got VSA?  Want to use your Prop-SX?  Now you can!  See the VSA section of the Library forum for Prop-SX code that works with VSA.


Realistic Candles

Started by JonnyMac, April 02, 2012, 06:46:52 PM

Previous topic - Next topic

JonnyMac

April 02, 2012, 06:46:52 PM Last Edit: April 03, 2012, 10:33:04 AM by JonnyMac
With the power of the HC-8+ and some libraries we've built (and borrowed), it's pretty easy to create very realistic candles using just an LED circuit (no capacitor is required).

In the code below (extracted from the full program -- see attached) we use an advanced randomizer to setup the initial values for the outputs and where they will go next (target value).  The program then drops into a loop that moves each output toward its target and when it hits the target it randomizes a new one.  I've setup the the values for standard candles; mostly one with about 20% brightness modulation.  Of course, this could be modified to create a wind effect, or slowed down and the range clamped to create a burning embers effect.

For those of you that are new this will look hard; it looks that way because you're new.  As you learn Spin (the programming language of the Propeller) you'll see that it's pretty simple; the hard part is in the libraries which you don't even have to understand -- they're just plug-and-play.

obj

  rr   : "realrandom"                                           ' random from hardware
  prng : "efx_prng"                                             ' advanced pseudo-random
  leds : "efx_pwm8"                                             ' pwm output for LEDs


var

  long  target[8]                                               ' target for each channel
 

pub main | ch, level, t 

  outa[OUT7..OUT0] := %0000_0000                                ' clear outputs
  dira[OUT7..OUT0] := %1111_1111                                ' output mode                                 
  low(OUT_EN)                                                   ' enable 74x245

  ' setup randomizer
  ' -- use hardware to seed advanced pseudo-random object
  ' -- excellent random without extra cog (loaded and then released)
                                                     
  rr.start                                                     
  prng.seed(rr.random, rr.random, rr.random, rr.random, rr.random)
  rr.stop
                                                       
  leds.start(8, OUT0)                                           ' start LED dimmer

  repeat ch from 0 to 7                                         ' seed the wicks
    leds.set(ch, prng.random // 55 + 201)                       ' light the led
    target[ch] := prng.random // 55 + 201                       ' set 1st target

  t := cnt                                                      ' synchronize loop timer
  repeat
    repeat ch from 0 to 7
      if (leds.read(ch) == target[ch])                          ' at target?
        target[ch] := prng.random // 55 + 201                   ' yes, set new one
      elseif (leds.read(ch) < target[ch])                       ' below target?
        leds.inc(ch)                                            ' yes, bump up
      else
        leds.dec(ch)                                            ' no, bump down

    ' minimum delay is 1400us
       
    waitcnt(t += (2_500 * US_001))                              ' run every 2.5ms
Jon McPhalen
EFX-TEK Hollywood Office

JonnyMac

My friend Jeff told me about this site: http://www.plasmaled.com/5mm8mmled.htm

The amber LED would probably work really well with the candles demo -- I ordered some to try.
Jon McPhalen
EFX-TEK Hollywood Office