May 18, 2024, 03:38:36 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.


Converting Haunted House Prop-1 Program to HC-8

Started by Spooky Dad, June 17, 2011, 08:18:35 PM

Previous topic - Next topic

Spooky Dad

JonnyMac - Could I get your help in writing up one method corresponding to how to program one of the props as an example for me?  I also don't know where to insert and read the last value from the cnfPins statement below.  Just give me a little more of push so that I can take a stab at the rest of the program, please.   :D

JonnyMac

July 31, 2011, 07:53:01 AM #16 Last Edit: July 31, 2011, 07:58:15 AM by JonnyMac
Let's do a bit more support work before building the control code.  In your application you have eight prop zones that require a retrigger timer (for each) and a scream timer when all is idle.  With the Propeller we can setup one of a cog (processor) to handle the timers independent of the rest of the program.

In the global variable section at the top we need to add an array for the timers, plus an array for a stack.

  long  timer[9]                                                ' re-trigger timers
  long  tstack[16]                                              ' stack for timers cog


Without getting into grunt details, a "stack" is an area of RAM used for temporary variables.  As we're going to be launching another Spin interpreter into its own cog, it needs some RAM to work with (for internal stuff).  Setting stack size is a bit of a black art; I know that 16 longs is enough for what we're doing here.

And here it is:

pri update_timers | t, idx                                      ' launch with cognew

'' Updates hold-off; each with 1ms resolution

  t := cnt                                                      ' sync loop timer
  repeat                                                        ' run forever
    waitcnt(t += MS_001)                                        ' wait 1ms
    repeat idx from 0 to 8                                      ' 8 zones + scream timer
      if (timer[idx] > 0)
        --timer[idx]


This looks like a regular method, but it's not.  If you look closely you'll see that when this gets called it runs in a non-breaking loop, so you never want to call this as a regular method.  What we want to do is launch it into its own cog -- where it can run forever without stopping our normal program code; we do that with cognew.  Note that I always mark methods like this as private (as a reminder not to call them normally).

Finally, here's the code to start the timers cog:

  cognew(update_timers, @tstack)                                ' run hold-off timers

Add this line after the .start methods for the serial objects, at the top of your main method.  At this point the timers array will be decremented every millisecond -- in the "background."   Here's how we would put it to use: Assume that after running the prop in zone 0 we want to wait at least 30 seconds before it can run again, even if there is a trigger input for it.  The code for zone 0 would be setup like this:

pub zone_0

  if (timer[0] == 0)                                            ' if timer expired
    ' run prop                                                  '  run prop
    timer[0] := 30_000                                          '  set hold-off to 30s


Now you can see why we define the timer array as global; this lets any Spin method have access, including Spin methods running in other cogs! 





Jon McPhalen
EFX-TEK Hollywood Office

Spooky Dad

I'll review tonight and upload the updated program.  Thanks Jon!

JonnyMac

July 31, 2011, 11:43:10 AM #18 Last Edit: August 01, 2011, 10:20:15 PM by JonnyMac
Attached is my version of your program that includes reading the triggers and running the MIB portion of the program.  This is a LOT to study.  Please study before we move on.

This is what the MIB section looks like in the context of the program:

pub mib | lotto, runtime, delay

  if (holdoff[0] > 0)
    return                                                      ' abort if timer still running

  ' run MIB prop

  if (ina[MODE_SW] == SPOOKY)

    outa[CH0] := IS_ON                                          ' pop up
    play_ap8(%00, 1)                                            ' play sound
    pause(2_000)                                                ' wait 2s
    set_relay(0, IS_ON)                                         ' activate RC-4 K1

    runtime := 7_000                                            ' set local timer
    repeat
      ?lotto                                                    ' stir random #
      outa[CH1..CH0] := lotto                                   ' update outputs
      delay := ||(?lotto) // 151 + 100                          ' 100 to 250ms
      pause(delay)                                              ' hold
      runtime -= delay                                          ' update timer
      if (runtime =< 0)                                         ' when expired
        quit                                                    '  quit loop

    outa[CH1..CH0] := IS_OFF                                    ' clear outputs
    set_relay(0, IS_OFF)                                        ' kill spotlight

    holdoff[0] := ||(?lotto) // 10_001 + 10_000                 '  hold-off, 10.000s to 20.000s

  else ' non-spooky

    outa[CH1..CH0] := %01                                       ' CH0 on, CH1 off

    repeat 10
      pause(2_000)
      !outa[CH1..CH0]                                           ' toggle bits

    outa[CH1..CH0] := IS_OFF                                    ' clear outputs


Note that I made some structural changes to make the code easier to read.  I also added a few more support routines.  This will happen as you develop complex programs.
Jon McPhalen
EFX-TEK Hollywood Office

Spooky Dad

Jon - Thanks for all of your help in my education with Propeller.  I have ordered the Prop-2 for this season and will be posting a new thread tonight to get your help to convert my program from a Prop-1 to the Prop-2.  We'll pick this up in 2012.