November 23, 2024, 11:56:22 AM

News:

You can now use Vixen to program your Prop-1 and Prop-2 controllers!  Get started quickly and easily, without having to learn PBASIC.  Details in the Library forum.


One More Candles Program

Started by JonnyMac, April 26, 2012, 10:50:13 AM

Previous topic - Next topic

JonnyMac

For a make-n-take demo I created a new version of my HC-8+ candles program.  This runs 8 wicks (OUT0..OUT7) and can use a normally-open button on the SERIAL header to create a door/window open effect.  Note that the SERIAL pin is an active-low input so the normally-open button goes between SER.W and SER.B.

On start up -- and if the button is open -- the wicks gently fade on and do their normal thing.  Press the button and things go a bit berserk.  Release the button and things  go back to normal.  The transition from wild back to calm works because of the tracking-to-target algorithm used in the candles simulation.


pub main | ch, bits, delay

  setup_io                                                      ' setup HC-8+ IO pins

  rr.start                                                      ' start hardware random     
  prng.seed(rr.random, rr.random, rr.random, rr.random, 0)      ' seed software random
  rr.stop                                                       ' kill hardward random

  ' start LED dimmer control
                                                       
  leds.start(8, OUT0)                                           ' start LED dimmer cog

  repeat ch from WICK1 to WICK8                                 ' seed the wicks
    target[ch] := prng.random // 55 + 201                       ' set 1st target

  repeat
    if (ina[TRIGGER] == TR_YES)                                 ' disturbed?
      bits := prng.random                                       ' randomize
      repeat ch from WICK1 to WICK8
        if (bits & (1 << ch))                                   ' high?
          level[ch] := (prng.random >> 1) // 51 + 50            ' 50 to 100
        else
          level[ch] := (prng.random >> 1) // 26 + 25            ' 25 to 50
        leds.set(ch, level[ch])

      delay := ((prng.random >> 1) // 51 + 50)                  ' 50 to 100ms
      pause(delay)
     

    else                                                        ' normal
      brightness := brightness + 1 <# 255                       ' ramp up if down
      repeat ch from WICK1 to WICK8
        if (level[ch] == target[ch])                            ' at target?           
          target[ch] := prng.random // 25 + 201                 '  yes, set new one     
        elseif (level[ch] < target[ch])                         ' below target?         
          level[ch] := ++level[ch] <# 255                       '  increase and limit   
        else                                                    ' above target         
          level[ch] := --level[ch] #> 0                         '  decrease and limit   
        leds.set(ch, level[ch] * brightness / 255)              ' set channel
       
      pause(8)                                                  ' ** adjust for normal **



Jon McPhalen
EFX-TEK Hollywood Office

JonnyMac

For those of you starting to experiment with Spin, you may be wondering what's going on with this:

  prng.random >> 1

That call returns a random number, which could be negative, and we don't want negative values.  By shifting the value right by one bit, we put a 0 in BIT31 of the value which will make it positive; everything is hunky-dorey from there.
Jon McPhalen
EFX-TEK Hollywood Office