November 22, 2024, 07:14:08 PM

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.


Newbie with a cool prop but programming dummy!

Started by RosehallManor, September 16, 2008, 10:47:39 AM

Previous topic - Next topic

RosehallManor

I have an ambulance cot that I am working with.  This year I have devised a way to make it shake, and I have set it up so that the "victim " on the cot will sit up.  Both using 24 V relay or 24 V solenoid.  I think if I could get the cot to shake for like three seconds and then sit up and hold for say five seconds that would be cool.  I will be using a PIR as a triggerwith a one or two second delay after the trigger before the prop activates.  I am trying to learn PBASIC, but it is coming slow.  Oh yeah did I mention my hard drive crashed and I lost my programming that I have done for the Animated Lighting portion of my display.  I am experimenting today with the PBASIC, but if anyone has some suggestions I would appreciate it.  I have read the documentation on the PIR and extra coding to avoid false triggers and I think that is where my confusion has started.   ???

Thanks all


JonnyMac

For the record, newbies get no sympathy from me!  :P 

Here you go:

' =========================================================================
'
'   File...... Ambulance_Cot.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'              Copyright (c) 2008 EFX-TEK
'              Some Rights Reserved
'              -- see http://creativecommons.org/licenses/by/3.0/
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated... 16 SEP 2008
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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

SYMBOL  PIR             = PIN6                  ' SETUP = DN
SYMBOL  Sitter          = PIN1
SYMBOL  Shaker          = PIN0


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

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

SYMBOL  IsUp            = 1
SYMBOL  IsDown          = 0


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

SYMBOL  delay           = W3
SYMBOL  timer           = W4
SYMBOL  lottery         = W5


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

Reset:
  PINS = %00000000                              ' clear all outputs
  DIRS = %00000011                              ' make P0-P1 outputs


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

Main:
  timer = 0                                     ' reset timer

Check_Trigger:
  RANDOM lottery                                ' stir random value
  PAUSE 5                                       ' loop pad
  timer = timer + 5 * PIR                       ' update timer
  IF timer < 100 THEN Check_Trigger             ' wait for 0.1 sec input

  delay = lottery // 1001 + 1000                ' random, 1 to 2 secs
  PAUSE delay

  timer = 0

Shake_It_Baby:
  Shaker = IsOn - Shaker                        ' toggle shake output
  RANDOM lottery                                ' re-stir
  delay = lottery // 251 + 250                  ' 0.25 to 0.5 secs
  PAUSE delay
  timer = timer + delay
  IF timer < 3000 THEN Shake_It_Baby
    Shaker = IsOff

  Sitter = IsUp
  PAUSE 5000

  GOTO Reset                                    ' everything off


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


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


' -----[ User Data ]-------------------------------------------------------
Jon McPhalen
EFX-TEK Hollywood Office

RosehallManor

Wow, thanks!  I will have to look this over a bunch of times to understand all that it is doing.  Below is what I came up with.  Perhaps you could give a short description of what yours is doing compared to mine.  I would really appreciate it.  It will help me learn!!!

=========================================================================
'
'   File...... COT.bs1
'   Purpose... cot animation
'   Author.... Matt Starr
'   E-mail.... mstarr@southelginfire.com
'   Started... September 16 2008
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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

SYMBOL  Sio             = 7                     ' SETUP = out; no ULN
SYMBOL  PIR         = PIN6                  ' SETUP = DN
SYMBOL  DRILL       = 0
SYMBOL  CYLINDER    = 1

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

SYMBOL  IsOn            = 1                     ' for ative-high input
SYMBOL  IsOff           = 0


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


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

Reset:


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

Main:
  IF PIR = IsOff THEN Main                  ' wait for trigger
  PAUSE 3000
  HIGH DRILL
  PAUSE 3000
  LOW DRILL
  PAUSE 2000
  HIGH CYLINDER
  PAUSE 5000
  LOW CYLINDER
  PAUSE 15000

  GOTO Main


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


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


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


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


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


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


' -----[ EEPROM Data ]-----------------------------------------------------

JonnyMac

September 19, 2008, 09:52:15 PM #3 Last Edit: September 19, 2008, 11:18:02 PM by JonnyMac
I'll give a very brief description of each section; you study it line-by-line to work out the inner details (it will be good for you, like eating veggies!).  And as you can imagine, with five weeks until Halloween I really don't have time to give a detailed explanation of every program I write.

Tip for learning PBASIC: double-click on a keyword (editor highlights them blue) and then press [F1] -- that will take you to the page in the help file.  The only drag is that PBASIC 1 and 2 keywords are mixed, but the PBASIC 1 is always at the top of any help file page.

Okay, here goes a quickie explanation:


Reset:
  PINS = %00000000                              ' clear all outputs
  DIRS = %00000011                              ' make P0-P1 outputs


This sets up the IO making P0 and P1 outputs and turning them off (i.e., LOW).  This code bit will run on power-up or if you press the reset button (which is why I name the label, "Reset").


Main:
  timer = 0                                     ' reset timer

Check_Trigger:
  RANDOM lottery                                ' stir random value
  PAUSE 5                                       ' loop pad
  timer = timer + 5 * PIR                       ' update timer
  IF timer < 100 THEN Check_Trigger             ' wait for 0.1 sec input


The primary purpose of this section is to debounce the PIR/trigger input.  By using a bit of combinatorial logic the timer variable is updated; it either has five added to it (when the PIR input is on, or "1") or it is cleared to zero (when the PIR input is off, or "0").  This works because the last step on that line multiplies the updated value of timer by 1 (PIR on) or 0 (PIR off).  The loop will run until the PIR input is on long enough for timer to reach 100 (milliseconds) -- 0.1 seconds.

Another thing this code does is stir the random value, lottery, so that it is truly random when we get a valid trigger input.


  delay = lottery // 1001 + 1000                ' random, 1 to 2 secs
  PAUSE delay


This uses the random value in lottery to create a delay between 1000 and 2000 milliseconds.  I've written about using the modulus (//) operator in this thread: http://www.efx-tek.com/php/smf/index.php?topic=49.0


  timer = 0

Shake_It_Baby:
  Shaker = IsOn - Shaker                        ' toggle shake output
  RANDOM lottery                                ' re-stir
  delay = lottery // 251 + 250                  ' 0.25 to 0.5 secs
  PAUSE delay
  timer = timer + delay
  IF timer < 3000 THEN Shake_It_Baby
    Shaker = IsOff


This is the meat of it -- and I wrote about this kind of code here: http://www.efx-tek.com/php/smf/index.php?topic=41.0

This really should be obvious; it flips the shaker output (from off to on, or on to off), re-stirs the random value (so it changes each time through) and then uses that value -- just like we did above -- to create a delay.  That delay gets added into a running timer and when that timer hits 3000 we turn the output off and move on in the program.


  Sitter = IsUp
  PAUSE 5000

  GOTO Reset                                    ' everything off


Okay, no explanation is needed here, right?


Please understand that I've been programming for 28 years and it's just second nature to me.  Still, since most of my programs are for public consumption I do my best to make them practical and easy to understand.  You won't get as good as me by this Halloween, but you could be by next if you spend a little time each day experimenting.  I do.  Every morning before I get busy with real work I spend about an hour experimenting, often writing code for someone else.

And that's a really good tip: If you want to become a good programmer start writing programs for others.  Between now and Halloween there will be a lot of requests for programs in these forums and there is no reason why I have to write them all.  If you want to learn to swim you've got to get wet!  :D
Jon McPhalen
EFX-TEK Hollywood Office

RosehallManor

OK, I've gone through the program and I want to add a "Pause 15000" at the end to prevent the prop from being continuously activated.  After the pause 5000, would I add a command of:

Sitter = IsOff

Pause 15000

Reset

Is the above correct for what I want to do?? 

livinlowe

 The Pause 5000 is a 5 second pause. If you want a 15 second pause just change it to say Pause 15000. Or you could have 3 Pause 5000, or a for next loop with pause 5000 inside the loop and loop it 3 times. If you add a pause 15000 after the pause 5000 you will have a 20 second pause.
Shawn
Scaring someone with a prop you built -- priceless!

JonnyMac

The better place to insert that delay is at the beginning, because you want everything to be off (Reset section handles this) in between prop activations.  Leave the program I originally wrote it but add one line to the Reset section:

Reset:
  PINS = %00000000                              ' clear all outputs
  DIRS = %00000011                              ' make P0-P1 outputs

  PAUSE 15000                                   ' inter-show delay
Jon McPhalen
EFX-TEK Hollywood Office