November 18, 2024, 01:23:28 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.


my first attempt at a for next and go sub

Started by gadget-evilusions, March 12, 2008, 09:28:54 PM

Previous topic - Next topic

gadget-evilusions

I must be missing something really simple. I want my program to trigger the valve (Out0) for 10 seconds when the trigger on P6 is activated or for 10 seconds once every 5 minutes, even when the trigger is not activated. Right now it will work when the trigger is activated but won't activate every 5 minutes.

' =========================================================================
'
'   File...... Evilusions_TCT_5min_loop.BS1
'   Purpose... Activate Valve for 10 seconds when trigger is pushed or once every 5 minutes.
'   Author.... Brian Warner, Evilusions (www.evilusions.com)
'   E-mail.... gadget@evilusions.com
'   Started... 3-12-08
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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


SYMBOL  Trigger         = PIN6
SYMBOL  Valve           = PIN0


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

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

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

SYMBOL  tix            = B2

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


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

Reset:
  DIRS = %00000001                              ' make P0 an output

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

Main:
Valve = IsOn
PAUSE 10000
Valve = IsOff
GOSUB Delay

Delay:
FOR tix = 1 TO 30000
IF Trigger = IsOn THEN Main
PAUSE 10
NEXT
GOTO Main


' -------------------------------------------------------------------------
Brian
Evilusions LLC
www.evilusions.com for all your pneumatic components

JonnyMac

March 12, 2008, 10:29:46 PM #1 Last Edit: March 13, 2008, 07:56:27 AM by JonnyMac
Removed by Jon because he was very tired when he posted and made a mistake....  Sorry, gang.
Jon McPhalen
EFX-TEK Hollywood Office

gadget-evilusions

Thanks. That seems a little more efficent.

I take it mine was a really wrong way of doing what I wanted?
Brian
Evilusions LLC
www.evilusions.com for all your pneumatic components

JonnyMac

March 13, 2008, 08:00:45 AM #3 Last Edit: March 13, 2008, 09:19:25 AM by JonnyMac
Brian,

Forgive me for writing demo code when I'm dragging-my-nuckles tired.  My program was not correct -- it would only delay 30 seconds, not five minutes.  Modify your code like this:

' =========================================================================
'
'   File...... Evilusions_TCT_5min_loop.BS1
'   Purpose... Activate Valve for 10 seconds when trigger is pushed or once every 5 minutes.
'   Author.... Brian Warner, Evilusions (www.evilusions.com)
'   E-mail.... gadget@evilusions.com
'   Started... 3-12-08
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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


SYMBOL  Trigger         = PIN6
SYMBOL  Valve           = PIN0


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

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

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

SYMBOL  tix             = W5

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


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

Reset:
  DIRS = %00000001                              ' make P0 an output

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

Main:
  FOR tix = 1 TO 30000
    IF Trigger = IsOn THEN Run_Valve
    PAUSE 10
  NEXT

Run_Valve:
  Valve = IsOn
  PAUSE 10000
  Valve = IsOff
  GOTO Main

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


You don't need GOSUB, and since you had no RETURN in the program that was creating problems.  You also need to change the definition of tix to a word (use W5) so that you can have it count up to 30000.  This modification is a little safer, I think, as it doesn't cause the prop to be activated on power-on, you need a trigger or five-minute delay.

Again, sorry for my mistake.
Jon McPhalen
EFX-TEK Hollywood Office

JonnyMac

In the event you want to debounce the trigger input (to prevent false triggers) you can update the code like this:

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

SYMBOL  idx             = B2
SYMBOL  timer           = B3

SYMBOL  tix             = W5


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


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

Reset:
  DIRS = %00000001                              ' make P0 an output


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

Main:
  FOR tix = 1 TO 3000
    timer = 0
    FOR idx = 1 TO 10                           ' debounce trigger
      timer = timer + 10 * Trigger
      PAUSE 10
    NEXT
    IF timer > 50 THEN Run_Valve
  NEXT

Run_Valve:
  Valve = IsOn
  PAUSE 10000
  Valve = IsOff
  GOTO Main


Haunts an electrically noisy and this is a better way to monitor the trigger.  Note, though, than neither of the programs have any hold-off after the valve is turned off which means the prop could be immediately retriggered -- you may want to add a delay.
Jon McPhalen
EFX-TEK Hollywood Office

gadget-evilusions

Brian
Evilusions LLC
www.evilusions.com for all your pneumatic components