November 22, 2024, 05:04:06 PM

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.


Flickering and IF-THEN on a Prop-2

Started by Liam, June 23, 2012, 11:44:48 PM

Previous topic - Next topic

Liam

Hi all,

I have finally mustered up enough courage to break out the Prop-2 that has been sitting in my box of EFX-TEK stuff for a couple of years, and am actually feel pretty good about my progress. I have created a flicker using an FC-4 that works just fine, but would like to randomly pause the flickering at different brighnesses periodically. Here's what the program does right now:

While waiting for a button press (no debounce since it's a button), it flickers a light on channel 1 of the FC-4. Once the button is pressed, it does the short routine of setting channel 1 of the FC-4 to 100 for 2 seconds, then 200 for 2 seconds, then returns to the main program. This all works fine.

Where I am getting stuck is trying to get the flicker to pause. During the sequence, I pull a random value for the "hold" variable. I would like to use this to determine if the program will go to Hold_Flicker or not using a chosen value (currently set to 2, so maybe 10% of the time the flicker will hold at a steady brightness for a random interval). The random interval should be the randomized value for the "delay" variable times 1000, so should end up being a few seconds. This does not seem to be happening, and I'm not sure why.

I'm going to keep going at it, but thought I'd post it here also in case someone seems what I'm missing. Thank you, as always!

Liam


' =========================================================================
'
'   File......BS2Flicker1.bs2
'   Purpose...Flicker AC bulb on channel 1 using an FC-4
'   Author....Liam Ferris
'   E-mail....lpferris@gmail.com
'   Started...6-22-2012
'   Updated...6-22-2012
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


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


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


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

Sio             PIN     15                      ' SETUP = UP; no ULN
Trigger         PIN     14                      ' SETUP = DN

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

IsOn            CON     1                       ' for active-high in/out
IsOff           CON     0

#SELECT $STAMP
  #CASE BS2, BS2E, BS2PE
    T2400       CON     396
    T38K4       CON     6
  #CASE BS2SX, BS2P
    T2400       CON     1021
    T38K4       CON     45
  #CASE BS2PX
    T2400       CON     1646
    T38K4       CON     84
#ENDSELECT

Open            CON     $8000
Baud            CON     Open + T38K4            ' B/R jumper installed


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

timer           VAR     Word
flame1          VAR     Word
fire1           VAR     Byte
delay           VAR     Byte
hold            VAR     Nib
hold_flick      VAR     Word


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

Reset:
  OUTH = %00000000 : OUTL = %00000000           ' preset IOs
  DIRH = %00000000 : DIRL = %00000000           ' set outputs (1s)

  SEROUT Sio, Baud, ["!FC4", %11, "X"]          ' reset FC-4



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

Main:
  DO WHILE (trigger < 1)                        ' wait for trigger input
    RANDOM delay
    PAUSE delay
    RANDOM hold
    RANDOM fire1
    fire1 = fire1 * 150 / 255
    SEROUT Sio, Baud, ["!FC4", %11, "L", 1, fire1]
    IF (hold  = 2) THEN Hold_Flicker
    RANDOM delay
    RANDOM fire1
  LOOP

  SEROUT Sio, Baud, ["!FC4", %11, "L", 1, 100]
  PAUSE 2000
  SEROUT Sio, Baud, ["!FC4", %11, "L", 1, 200]
  PAUSE 2000

  GOTO Reset

Hold_Flicker:
  DEBUG "Hold Flicker"
  delay = delay * 1000
  PAUSE delay
  RETURN

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



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


JonnyMac

June 24, 2012, 09:23:18 AM #1 Last Edit: June 24, 2012, 09:25:08 AM by JonnyMac
One of the problems you may be having is that you define delay as a Byte and then try to multiply it by 1000 -- this will not get you where you want to go.  Another problem your program has is that you're using an implicit GOTO to a section of code that ends in RETURN; this will cause the program to go wonky.

In the end your requirements are somewhat sophisticated so your code will need to match.   Give this variation a try.  If it works, take a bit of time to study the code to see if you can determine why I did what I did -- it will prep you for even greater challenges later.  Of course, if something just doesn't ring for you, ask and I'll explain.

' =========================================================================
'
'   File...... BS2Flicker1_v2.BS2
'   Purpose... Flicker AC bulb on channel 1 using an FC-4
'   Author.... Liam Ferris with assist from JonnyMac
'   E-mail.... lpferris@gmail.com
'   Started... 6-22-2012
'   Updated... 6-23-2012
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


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


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


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

Sio             PIN     15                      ' SETUP = UP; no ULN
Trigger         PIN     14                      ' SETUP = DN


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

IsOn            CON     1                       ' for active-high in/out
IsOff           CON     0

#SELECT $STAMP
  #CASE BS2, BS2E, BS2PE
    T2400       CON     396
    T38K4       CON     6
  #CASE BS2SX, BS2P
    T2400       CON     1021
    T38K4       CON     45
  #CASE BS2PX
    T2400       CON     1646
    T38K4       CON     84
#ENDSELECT

Open            CON     $8000
Baud            CON     Open + T38K4            ' B/R jumper installed


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

delay           VAR     Word

lottery         VAR     Word
fire           VAR      lottery.LOWBYTE
hold           VAR      lottery.HIGHBYTE


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

Reset:
  OUTH = %00000000 : OUTL = %00000000           ' preset IOs
  DIRH = %00000000 : DIRL = %00000000           ' set outputs (1s)

  SEROUT Sio, Baud, ["!FC4", %11, "X"]          ' reset FC-4

  delay = 1031
  lottery = 1224


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

Main:
  RANDOM lottery                                ' stir random #s
  RANDOM delay

  IF (Trigger = IsOn) THEN
    SEROUT Sio, Baud, ["!FC4", %11, "L", 1, 100]
    PAUSE 2000
    SEROUT Sio, Baud, ["!FC4", %11, "L", 1, 200]
    PAUSE 2000
  ELSEIF (hold = 2) THEN
    RANDOM delay
    delay = ((delay & %111111) * 125) MIN 1000  ' 1000 to 7875ms
    PAUSE delay
  ELSE
    fire = (fire * 150) >> 8                    ' fire x 0.585
    SEROUT Sio, Baud, ["!FC4", %11, "L", 1, fire]
    RANDOM lottery
    PAUSE hold
  ENDIF

  GOTO Main


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


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


Jon McPhalen
EFX-TEK Hollywood Office

Liam

Jon, thank you very much. I will test this out later today. One thing I don't quite get are the equations you're using to calculate delay and fire, but I actually have a page bookmarked (I'm pretty sure it's one of yours) that describes all these fancy equations so I can look them up myself.

Also, it sounds like using a subroutine within the IF (trigger = IsOn) THEN section would be a bad idea. Is that correct?

Thanks again!

Liam

JonnyMac

One of the problems that we encounter is not having "specs" for a prop (which includes behavior and specific timing requirements) which forces us to guess with the information and code provided.  I did a lot of guessing and could have been off base.

My guess:
  -- if button is pressed
     * 100 for 2s
     * 200 for 2s
     * back to beginning

  -- if button not pressed and hold is 2
     * create random delay
     * hold at present value
     * back to beginning

  -- if button not pressed and hold is not 2
     * create random flicker effect

Is that not what you wanted?

QuoteAlso, it sounds like using a subroutine within the IF (trigger = IsOn) THEN section would be a bad idea. Is that correct?

Subroutines are best when you need to use the same section of code from different parts of a program -- that is not the case here.  You could, though, like this:

  IF (Trigger == IsOn) THEN
    GOSUB SubroutineName
  ENDIF


Since the subroutine code is only used in one place I stuck it in the THEN block.

QuoteOne thing I don't quite get are the equations you're using to calculate delay and fire

You could if you worked them through, step by step.  Here's what's happening

  delay = ((delay & %111111) * 125) MIN 1000

This is the same as:

  delay = ((delay // 64) * 125) MIN 1000

By masking off all but the lower six bits we're left with a value between 0 and 63.  This is multiplied by 125 (1/8th second) and then the MIN operator is used to set the shortest time to one second.  This is one of those places where I didn't know what the hold timing should be so I took a SWAG.

  fire = (fire * 150) >> 8

is the same as

  fire = fire * 150 / 256

...which is very close to your 255 -- close enough that you'd not know the difference on output.

When possible, I avoid using *, **, /, and // as these are the least efficient operators for any microcontroller.  Small micros do not have built in multiply and divide instructions, so they have to be done in code and it takes a lot of code.  When I can I use shifting and masking; it's much more efficient.





 
Jon McPhalen
EFX-TEK Hollywood Office

Liam

No Jon, you were right on with your interpretation of my specs. I should have been more explicit in that at this point I'm trying to build out the idle behavior for my haunt intro show. The code in the triggered area is there as a placeholder, but most of the code that will be in that area will bet serial instructions to an AP-16+. I have it built out in another program, and it's working fine. It's not too complex, so I think it will not be a problem.

Thank you for the explanation of the equations. I also did some reading in the meantime, and that coupled with your explanation brings it all together. Thank you again, your expertise is always appreciated.

Liam

JonnyMac

My pleasure, Liam.  I'm glad you're getting an early start -- that means you'll have time for more fun!
Jon McPhalen
EFX-TEK Hollywood Office