November 23, 2024, 07:56:32 AM

News:

Be sure to checkout our Vixen interfaces in the Library forum -- if you want PC automation at near zero cost, EFX-TEK and Vixen is a great combination of tools.


Repeating

Started by Horndog, May 05, 2009, 03:36:20 PM

Previous topic - Next topic

Horndog

Okay, so I am fairly new to this, and I need to know how to repeat something on an infinite or set loop, can you help me?

JonnyMac

May 05, 2009, 03:45:16 PM #1 Last Edit: May 05, 2009, 03:46:48 PM by JonnyMac
GOTO is what you're looking for.  Let's say you wanted to turn on P0 for one second, leave it off for two seconds, then repeat this forever.  You might have code that looks like this:

Main:
  HIGH 0
  PAUSE 1000
  LOW 0
  PAUSE 2000
  GOTO Main


Of course, I think it's a good idea to name your pins with SYMBOL so that the listing is easier to comprehend for final function.  In this listing above we know that P0 is going on and off but we don't know what that means, functionally (is it a light flashing, a pop-up popping, etc.).

This will help: http://www.efx-tek.com/downloads/prop-1_programming_basics.pdf
Jon McPhalen
EFX-TEK Hollywood Office

Horndog

Okay, that works, is it possible to get it to start on a push of a button then repeat until you press the button again.

JonnyMac

Sure; now things get a little more involved.

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


' -----[ Program Description ]---------------------------------------------
'
' Trigger :: Parallax-compatible PIR or N.O. button (mat switch, etc.)
'            -- connect N.O. button between P6.W and P6.R


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


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

SYMBOL  Trigger         = PIN6                  ' SETUP = DN
SYMBOL  Lamp            = PIN0


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

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

SYMBOL  Yes             = 1
SYMBOL  No              = 0


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

SYMBOL  timer           = B2
SYMBOL  idx             = B3


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

Reset:
  PINS = %00000000                              ' clear all
  DIRS = %00000001                              ' set output pin(s)


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

Main:
  GOSUB Check_Trigger
  IF timer < 50 THEN Main

Flash:
  Lamp = IsOn
  PAUSE 500                                     ' on 0.5 seconds
  Lamp = IsOff
  PAUSE 1450                                    ' off 1.45 seconds
  GOSUB Check_Trigger                           '  + 0.05 seconds
  IF timer < 50 THEN Flash

Force_Release:
  GOSUB Check_Trigger
  IF timer > 0 THEN Force_Release
    GOTO Main


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

Check_Trigger:
  timer = 0
  FOR idx = 1 TO 10
    timer = timer + 5 * Trigger
    PAUSE 5
  NEXT
  RETURN

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


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

Horndog

Okay, I keep trying but I cannot get this to start by button and end by button!
(Side Note: Can I get it to go on from 0 TO 5, then off from 5 TO 0? (Rather than on from 0 TO 5 then off from 5 TO 0)

' =========================================================================
'
'   File....... Scroller.BS1
'   Purpose.... Linear streak of lights
'   Author..... Horndog
'   E-mail.....
'   Started.... 05 MAY 2009
'   Updated....
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------
'
' Creates a linear strobe of six lamps/LEDs on P0..P5; much like airport
' landing lights.  Strobing runs when button on P6 is pressed.


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


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

SYMBOL  Trigger         = PIN6                  ' SETUP = DN


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

SYMBOL  IsOn            = 1                     ' button pressed
SYMBOL  IsOff           = 0                     ' button released

SYMBOL  LedTime         = 75                    ' light-on delay time


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

SYMBOL  thePin          = B2                    ' pin pointer



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

Reset:



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

Main:
  IF Trigger = IsOff THEN Main               ' wait for button press

  FOR thePin = 0 TO 5                        ' loop through LEDs
    PAUSE LedTime                              ' hold
    HIGH thePin                                ' LED on
  NEXT

  FOR thePin = 0 TO 5                          ' loop through LEDs
    LOW thePin                                  ' LED off
    PAUSE LedTime                               ' hold
  NEXT

  GOTO Main










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


' -----[ DATA Data ]-------------------------------------------------------

livinlowe

Try this:


Main:
  IF Trigger = IsOff THEN Main               ' wait for button press

Loop:
  FOR thePin = 0 TO 5                        ' loop through LEDs
    PAUSE LedTime                              ' hold
    HIGH thePin                                ' LED on
  NEXT

  FOR thePin = 0 TO 5                          ' loop through LEDs
    LOW thePin                                  ' LED off
    PAUSE LedTime                               ' hold
  NEXT

IF Trigger = IsOff THEN Loop             ' wait for button press

GOTO Main


Shawn
Scaring someone with a prop you built -- priceless!

JonnyMac

Here is how I'd do it -- this handles button debouncing and human timing issues.

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


' -----[ Program Description ]---------------------------------------------
'
' Trigger :: Parallax-compatible PIR or N.O. button (mat switch, etc.)
'            -- connect N.O. button between P6.W and P6.R


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


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

SYMBOL  Trigger         = PIN6                  ' SETUP = DN


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

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

SYMBOL  Yes             = 1
SYMBOL  No              = 0


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

SYMBOL  flags           = B0
SYMBOL   runOk          =  BIT0

SYMBOL  timer           = B2
SYMBOL  idx             = B3
SYMBOL  pinNum          = B4


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

Reset:
  PINS = %00000000                              ' clear all
  DIRS = %00111111                              ' set output pins

  PAUSE 1000


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

Main:
  GOSUB Check_Trigger
  IF runOk = No THEN Main

Force_Release:
  IF Trigger = IsOn THEN Force_Release

Zip_Fwd:
  FOR pinNum = 0 TO 4
    HIGH pinNum
    GOSUB Check_Trigger
    IF runOk = No THEN Reset
    PAUSE 50
    LOW pinNum
  NEXT

Zip_Back:
  FOR pinNum = 5 TO 1 STEP -1
    HIGH pinNum
    GOSUB Check_Trigger
    IF runOk = No THEN Reset
    PAUSE 50
    LOW pinNum
  NEXT

  GOTO Zip_Fwd


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

' Checks trigger input; when valid it flips the run/stop bit
' -- consumes 50ms

Check_Trigger:
  timer = 0
  FOR idx = 1 TO 10
    timer = timer + 5 * Trigger
    PAUSE 5
  NEXT
  IF timer < 50 THEN Trigger_Exit
    flags = flags ^ %00000001                   ' toggle run/stop bit

Trigger_Exit:
  RETURN


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