November 22, 2024, 12:30:51 AM

News:

Be sure to checkout our Vixen interfaces in the Library forum -- if you want PC automation at near zero cost, EFX-TEK and Vixen is a great combination of tools.


Random Fader

Started by imagineerdan, April 30, 2008, 11:11:15 AM

Previous topic - Next topic

imagineerdan

I am trying to do a PWM which fades on and off and pauses randomly, (for a different kind of flame look) How would I do this. I tryed using the random feature and byte but nothing was working. How would it look. I am ideally pwming from 20 - 45 as opposed to 0-255. Thanks!

JonnyMac

April 30, 2008, 12:19:12 PM #1 Last Edit: April 30, 2008, 12:22:52 PM by JonnyMac
Let me just say right up front and out loud: trying to create a realistic flame effect is darned hard; I know because I've recently designed an electronic candle for a very famous amusement/entertainment company here in SoCal.  It does not use a BASIC Stamp; it's a very small, stand-alone device and the modulation requirements to create a realistic flame effect (the light cast, not the flame itself) was tough, which meant I had to use an SX chip for LED modulation (the wick has three).  In our final demo it was compared -- side-by-side -- with light cast by a real candle.  Before anyone asks, no, this will not be sold publicly -- it's too expensive for home haunters.

Try this BS2 program; it uses a bit of the strategy I employed in my SX-based wick.

' =========================================================================
'
'   File...... Flame_LED.BS2
'   Purpose...
'   Author.... Jon Williams, EFX-TEK (www.efx-tek.com)
'   E-mail.... jwilliams@efx-tek.com
'              Copyright (c) 2008 EFX-TEK
'              Some Rights Reserved
'              see: http://creativecommons.org/licenses/by/3.0/us/
'   Started...
'   Updated...
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

Led             PIN      8


' -----[ Constants ]-------------------------------------------------------


' -----[ Variables ]-------------------------------------------------------

lottery         VAR     Word

delay           VAR     Byte
target          VAR     Byte
speed           VAR     Byte
level           VAR     Byte


' -----[ Initialization ]--------------------------------------------------

Reset:
  HIGH Led


' -----[ Program Code ]----------------------------------------------------

Main:
  RANDOM lottery
  delay = lottery // 15 + 15
  PAUSE delay

Flicker_Down:
  RANDOM lottery
  target = lottery // 128 + 64
  RANDOM lottery
  speed = lottery // 2 + 1
  FOR level = 255 TO target STEP -1
    PWM Led, level, speed
  NEXT

Flicker_Up:
  RANDOM lottery
  speed = lottery // 3 + 1
  FOR level = target TO 255
    PWM Led, level, speed
  NEXT
  HIGH Led

  GOTO Main


' -----[ Subroutines ]-----------------------------------------------------


' -------------------------------------------------------------------------


' -----[ User Data ]-------------------------------------------------------
Jon McPhalen
EFX-TEK Hollywood Office

JonnyMac

By the way, if you want to create a random value between 20 and 45, you would do this:

  RANDOM lottery
  level = lottery // 26 + 20


This technique is discussed in my "math tricks" thread.
Jon McPhalen
EFX-TEK Hollywood Office

Bill P

Quote from: JonnyMac on April 30, 2008, 12:20:35 PM
By the way, if you want to create a random value between 20 and 45, you would do this:

  RANDOM lottery
  level = lottery // 26 + 20


This technique is discussed in my "math tricks" thread.

Jon: Where is this thread, it sounds interesting?

Hope I'm not posting in the wrong place to ask this question.

I can see how 26 + 20 equals 20 to 45 random value, but not why.

Thanks, Bill

JonnyMac

Math tricks thread: http://www.efx-tek.com/php/smf/index.php?topic=49.0

The technique works because the modulus operator (//) always returns a value between zero and the divisor minus one.
Jon McPhalen
EFX-TEK Hollywood Office