November 23, 2024, 04:17:44 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.


Two Props At Once -- With Triggers

Started by JonnyMac, April 11, 2012, 09:29:51 AM

Previous topic - Next topic

JonnyMac

Yesterday I posted a simple program that ran a set of candles and an RGB mixer -- two independent processes in one controller.  Let's take that a bit further.  What if we wanted to have props that respond to input triggers; can we do it?

You bet.  In this version I have a "background" (runs in own cog) mechanism that scans and debounces the digital inputs (TTL and DMX address switch) of the HC-8+.  The result variable is global which means it can be accessed by any cog in the program.  This means we can have multiple triggered props running inside the HC-8+.

Here's the bulk of the code.  The main() routine starts the background scanner, the LED dimmer, and launches the triggered chaser cog before dropping into the candles loop.  Both the chaser and candles respond to respective triggers.

pub main | t, ch

  setup_io                                                      ' setup HC-8+ IO pins

  rr.start                                                     
  prng.seed(rr.random, rr.random, rr.random, rr.random, rr.random)
  rr.stop
                                                       
  leds.start(8, OUT0)                                           ' start LED dimmer cog

 
  ' run chaser in separate cog
 
  cognew(chaser, @stack0)                                       ' start chaser cog

 
  ' run candles in this cog

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

  brightness := 0                                               ' start off

  t := cnt                                                      ' synchronize timer
  repeat                                                        ' run forever
    repeat ch from WICK1 to WICK5                               ' loop through wicks
      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
      if ((brightness < 75) or (ttl_bit(7) == NO))              ' fading up or IN7 low?
        brightness := ++brightness <# 255                       ' increase wicks level
      else 
        brightness := --brightness #> 75                        ' decrease wicks level
     
      waitcnt(t += (2_000 * US_001))                            ' wait 2.0ms 


con

  { =================================== }
  {                                     }
  {  S E C O N D A R Y   C O N T R O L  }
  {                                     }
  { =================================== }


pri chaser | ch                                                 ' launch with cognew

'' Triggered chaser
'' -- runs when IN0 is active (high)

  repeat
    if (ttl_bit(0) == YES)                                      ' IN0 active?
      repeat ch from CHASE1 to CHASE3                           ' loop through chaser leds
        leds.high(ch)                                           ' on
        pause(100)                                              ' short delay
        leds.low(ch)                                            ' off


For those of you that are starting to experiment with Spin... how many cogs does the program use?  Four.  The main program and candles runs in Cog 0, the inputs scanner in Cog 1, the LED PWM (dimmer) controller in Cog 2, and the chaser program runs in Cog 3.
Jon McPhalen
EFX-TEK Hollywood Office