November 24, 2024, 12:46:00 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.


LED programing question

Started by imagineerdan, September 24, 2007, 02:10:14 PM

Previous topic - Next topic

imagineerdan

I am using the attached program and want to tigger the leds to pulse, and pin4 to stay on for 500 milliseconds then turn off. I am trying using FOR and NEXT because I want the whole sequence to only repeat 10 times then go back to waiting for a trigger again, but I cannot get it to work. How would I do that?

JonnyMac

I'm having a bit of a time following the logic of the program; can you explain it to me again, it a timeline type manner -- what happens and when.
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

No Problem. The prop one is dormant, when pin 7 is triggered pin 4 goes high for .5 seconds then goes low, then the "pulsing LED" program runs for 30 seconds. Hopefully this is a little clearer, thanks soo much again.

JonnyMac

Can you define "pulsing" -- what do you want to see?  You'll have to use the Up/Down cycles as timing elements for the 500 ms activation of P4.
Jon McPhalen
EFX-TEK Hollywood Office

JonnyMac

Dan,

See if this works (note that my I/O assignments let me test on the Prop-1 Trainer).  Here's how it works: While waiting on the trigger the random value is constantly stirred -- this lets the program behave differently each time through.  Once triggered, the control output (P4) is activated and the cycles count is reset to zero.  The bulk of the program selects an LED output and if it's okay to run it will dim it or brighten it.  Here's the rub: the brightening and dimming are fixed at 250 ms to create loop timing that can be used for the control output; after the second loop (2 x 250 ms = 0.5 s) has run the Ctrl output is deactivated.  The program continues pulsing the LEDs until enough cycles have run to make 30 seconds (30,000 / 250 = 120).

It may be possible to create randomized brighten and dimming speeds -- but that will be even more tricky.  Let's see if this works for your application.


' =========================================================================
'
'   File...... Puffer.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK (based on work by ImagineerDan)
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]--------------------------------------------
'
' Puffer sequencer


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

SYMBOL  Trigger         = PIN6                  ' SETUP = DN
SYMBOL  Ctrl            = PIN4                  ' control output
SYMBOL  Lamp3           = PIN3
SYMBOL  Lamp2           = PIN2
SYMBOL  Lamp1           = PIN1
SYMBOL  Lamp0           = PIN0


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

SYMBOL  IsOn            = 1
SYMBOL  IsOff           = 0


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

SYMBOL  theLed          = B2                    ' selected output
SYMBOL  last            = B3                    ' last LED selected
SYMBOL  mask            = B4                    ' pin mask
SYMBOL  level           = B5                    ' PWM level
SYMBOL  cycles          = W4                    ' loop cycles run
SYMBOL  lottery         = W5                    ' random value


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

Reset:
  PINS = %00000000                              ' clear everything
  DIRS = %00011111                              ' P0 - P4 are outputs


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

Main:
  RANDOM lottery                                ' stir random value
  IF Trigger = IsOff THEN Main                  ' wait for trigger

  cycles = 0                                    ' reset cycles count
  Ctrl = IsOn                                   ' activate control output

Pulsate:
  IF cycles < 2 THEN Select_Led                 ' leave Ctrl on for 500 ms
    Ctrl = IsOff                                ' deactivate

Select_Led:
  RANDOM lottery                                ' re-stir random value
  theLed = lottery // 4                         ' select an LED
  IF theLed = last THEN Select_Led              ' no repeats
    last = theLed                               ' save for next time

Check_State:
  READ theLed, mask                             ' create mask for pin
  mask = PINS & mask                            ' isolate the pin
  IF mask > 0 THEN Dim_Led                      ' dim if LED is on

Brighten_Led:
  FOR level = 0 TO 250 STEP 5                   ' ramp up
    PWM theLed, level, 1
  NEXT
  HIGH theLed                                   ' keep on
  GOTO Finish_Up

Dim_Led:
  FOR level = 255 TO 5 STEP -5                  ' ramp down
    PWM theLed, level, 1
  NEXT
  LOW theLed                                    ' keep off
  GOTO Finish_Up

Finish_Up:
  cycles = cycles + 1                           ' update cycles
  IF cycles < 120 THEN Pulsate                  ' keep going?
    GOTO Reset                                  ' nope, we're done


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


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


' -----[ User Data ]-------------------------------------------------------

Mask_Bits:
  EEPROM (%00000001, %00000010, %00000100, %00001000)
Jon McPhalen
EFX-TEK Hollywood Office

JonnyMac

Okay, here's a version that randomizes the ramp up and ramp down speed -- it's a tad more complicated.  Part of the complication is that the Ctrl output will run at least 500 ms, but could run a bit longer based on how the code falls.  If it's okay for that to happen then this version may be more to your liking.  I also added a section at the end that ramps down an LEDs that are lit when the cycle time expires.

' =========================================================================
'
'   File...... Puffer_v2.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK (based on work by ImagineerDan)
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]--------------------------------------------
'
' Puffer sequencer


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

SYMBOL  Trigger         = PIN6                  ' SETUP = DN
SYMBOL  Ctrl            = PIN4                  ' control output
SYMBOL  Lamp3           = PIN3
SYMBOL  Lamp2           = PIN2
SYMBOL  Lamp1           = PIN1
SYMBOL  Lamp0           = PIN0


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

SYMBOL  IsOn            = 1
SYMBOL  IsOff           = 0


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

SYMBOL  theLed          = B2                    ' selected output
SYMBOL  last            = B3                    ' last LED selected
SYMBOL  mask            = B4                    ' pin mask
SYMBOL  level           = B5                    ' PWM level
SYMBOL  speed           = B6                    ' PWM speed
SYMBOL  cycleTm         = W4                    ' cycle timing
SYMBOL  lottery         = W5                    ' random value


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

Reset:
  PINS = %00000000                              ' clear everything
  DIRS = %00011111                              ' P0 - P4 are outputs


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

Main:
  RANDOM lottery                                ' stir random value
  IF Trigger = IsOff THEN Main                  ' wait for trigger

  cycleTm = 0                                   ' reset cycle timer
  Ctrl = IsOn                                   ' activate control output

Pulsate:
  IF cycleTm < 500 THEN Select_Led              ' leave Ctrl on for 500 ms
    Ctrl = IsOff                                ' deactivate

Select_Led:
  RANDOM lottery                                ' re-stir random value
  theLed = lottery // 4                         ' select an LED
  IF theLed = last THEN Select_Led              ' no repeats
    last = theLed                               ' save for next time

Check_State:
  READ theLed, mask                             ' create mask for pin
  mask = PINS & mask                            ' isolate the pin
  IF mask > 0 THEN Led_Is_On                    ' dim if LED is on

Led_Is_Off:
  GOSUB Brighten_Led
  GOTO Mark_Cycle

Led_Is_On:
  GOSUB Dim_Led

Mark_Cycle:
  IF cycleTm < 30000 THEN Pulsate               ' keep going?

Clean_Up:
  FOR theLed = 0 TO 3                           ' loop through LED pins
    READ theLed, mask                           ' create pin mask
    mask = PINS & mask                          ' test the LED
    IF mask = 0 THEN Already_Off                ' on?
      GOSUB Dim_Led                             ' yes, dim it off

Already_Off:
  NEXT

  GOTO Reset


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

Brighten_Led:
  RANDOM lottery
  speed = lottery // 8 + 3                      ' 3 to 10
  FOR level = 0 TO 255 STEP speed               ' ramp up
    PWM theLed, level, 1
    cycleTm = CycleTm + 5
  NEXT
  HIGH theLed                                   ' keep on
  RETURN

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

Dim_Led:
  RANDOM lottery
  speed = lottery // 8 + 3                      ' 3 to 10
  FOR level = 255 TO 0 STEP -speed              ' ramp down
    PWM theLed, level, 1
    cycleTm = CycleTm + 5
  NEXT
  LOW theLed                                    ' keep off
  RETURN


' -----[ User Data ]-------------------------------------------------------

Mask_Bits:
  EEPROM (%00000001, %00000010, %00000100, %00001000)
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

Jon that is beautiful, and I can change the duration very easily, thanks again!!

Best,

Daniel

JonnyMac

Excellent!  Now... what are you doing with that program?
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

I am working on my home haunt with and animated pumpkin prop, should be fun!

JonnyMac

Cool.  Are you in the "valley" (near Disney)?  I have a friend (who used to be an FX guy at Disney) who has a neat home haunt (in Glendale) that I'll be assisting with.  You can see his custom animatronics system at www.socalhalloween.com.
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

It works great on my prop one, but I am trying the code on the small and compact Rev Dx modual it it the leds all stay high all the time, it seems like its reading the code in reverse. When the pin is activated the PWM is ramps down to black which lasts only for a few milliseconds. Its bazaar, any ideas

JonnyMac

On the Prop-1 are you using the OUTx terminals?  I'm betting you are -- and signals from the processor are inverted through this chip (active-low output), which is why "common" for the OUTx terminals is positive. 

On your Rev D -- assuming you haven't installed a ULN --  connect the anode side of the LEDs to the  I/O pins, the cathode side to ground (with the proper current limiter, of course).
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

excellent, that was it!, now I am in need of modifying the code above a bit and am a bit lost as to how to do so. I want to basically double the triggers and actions. So now I have it now funtioning  when pin 7 is triggered pin 4 goes high for .5 seconds then goes low, then the "pulsing LED" program runs for 30 seconds. I would like to have it when pin 7 is triggered the above happens, but when pin 6 is triggered, pin 5 goes high for .5 seconds, then the pulsing led program runs in more of a candle flicker in this state.

So to sum up

pin7 triggered, pins 0-3 pulsate and pin4 goes high for 500 milliseconds, all lasting for 30 seconds untill triggered again bby pin 7

pin6 is triggered then pins 0-3 flicker more like a candle and pin5 goes high for 500 milliseconds, which just like above will cycle for 30 seconds until triggered again.

Thanks again Jon!


JonnyMac

See if this works -- you may need to adjust the values into "span" and "spdMin" for the candle flicker effect.

' =========================================================================
'
'   File...... Puffer_v3.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK (based on work by ImagineerDan)
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]--------------------------------------------
'
' Puffer sequencer


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

SYMBOL  Trigger1        = PIN7                  ' SETUP = DN
SYMBOL  Trigger2        = PIN6                  ' SETUP = DN
SYMBOL  Ctrl1           = PIN5                  ' control output
SYMBOL  Ctrl2           = PIN4
SYMBOL  Lamp3           = PIN3
SYMBOL  Lamp2           = PIN2
SYMBOL  Lamp1           = PIN1
SYMBOL  Lamp0           = PIN0


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

SYMBOL  IsOn            = 1
SYMBOL  IsOff           = 0


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

SYMBOL  theLed          = B0                    ' selected output
SYMBOL  last            = B1                    ' last LED selected
SYMBOL  mask            = B2                    ' pin mask
SYMBOL  level           = B3                    ' PWM level
SYMBOL  speed           = B4                    ' PWM speed
SYMBOL  span            = B5                    ' for speed selection
SYMBOL  spdMin          = B6                    ' for speed selection
SYMBOL  cycleTm         = W4                    ' cycle timing
SYMBOL  lottery         = W5                    ' random value


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

Reset:
  PINS = %00000000                              ' clear everything
  DIRS = %00111111                              ' P0 - P5 are outputs

  cycleTm = 0


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

Main:
  RANDOM lottery                                ' stir random value
  IF Trigger1 = IsOn THEN Show_1                ' wait for trigger
  IF Trigger2 = IsOn THEN Show_2
  GOTO Main

Show_1:
  Ctrl1 = IsOn                                  ' activate control output
  span = 8                                      ' for slow pulsing
  spdMin = 3
  GOTO Pulsate

Show_2:
  Ctrl2 = IsOn                                  ' activate control output
  span = 64                                     ' for candle-like flicker
  spdMin = 64

Pulsate:
  IF cycleTm < 500 THEN Select_Led              ' leave Ctrl on for 500 ms
    Ctrl1 = IsOff                               ' deactivate
    Ctrl2 = IsOff

Select_Led:
  RANDOM lottery                                ' re-stir random value
  theLed = lottery // 4                         ' select an LED
  IF theLed = last THEN Select_Led              ' no repeats
    last = theLed                               ' save for next time

Check_State:
  READ theLed, mask                             ' create mask for pin
  mask = PINS & mask                            ' isolate the pin
  IF mask > 0 THEN Led_Is_On                    ' dim if LED is on

Led_Is_Off:
  GOSUB Brighten_Led
  GOTO Mark_Cycle

Led_Is_On:
  GOSUB Dim_Led

Mark_Cycle:
  IF cycleTm < 30000 THEN Pulsate               ' keep going?

Clean_Up:
  FOR theLed = 0 TO 3                           ' loop through LED pins
    READ theLed, mask                           ' create pin mask
    mask = PINS & mask                          ' test the LED
    IF mask = 0 THEN Already_Off                ' on?
      GOSUB Dim_Led                             ' yes, dim it off

Already_Off:
  NEXT

  GOTO Reset


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

Brighten_Led:
  RANDOM lottery
  speed = lottery // span + spdMin
  FOR level = 0 TO 255 STEP speed               ' ramp up
    PWM theLed, level, 1
    cycleTm = CycleTm + 5
  NEXT
  HIGH theLed                                   ' keep on
  RETURN

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

Dim_Led:
  RANDOM lottery
  speed = lottery // span + spdMin
  FOR level = 255 TO 0 STEP -speed              ' ramp down
    PWM theLed, level, 1
    cycleTm = CycleTm + 5
  NEXT
  LOW theLed                                    ' keep off
  RETURN


' -----[ User Data ]-------------------------------------------------------

Mask_Bits:
  EEPROM (%00000001, %00000010, %00000100, %00001000)
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

Now for the next step in this pumkin code,

I am trying to use a 38 khz IC to trigger pin7 in with this code and a 455 khz IC to trigger pin6 (using the above code)


This is so the pumkin is triggered by a (38 Khz remote repeater) and does its first show routine, then when the pumpkin is takin to another location (where there will be a 455 khz Ir repeater, the other pin will be triggered and the pumpkin will do a whole other routine. How would I wire these IC's into the pin inputs for 6 and 7.

Both IR recievers are three pin standard ic units.

And would the code have to be modified?