November 22, 2024, 01:57:37 AM

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.


For...Next in BS2

Started by imagineerdan, December 13, 2007, 06:09:51 PM

Previous topic - Next topic

imagineerdan

I am trying to get this code to run for 4 seconds then move on to the next part of the code. I am having a bit of trouble getting it to count, I am not sure if I am using the correct command or layout. How wouls I make this loop for 4 seconds then goto another section?

cycleTm = 0

Main2:

Select_Lamp:
OUTS = %0111111111111110                       ' all on

cycleTm = cycleTm + 5


  RANDOM lottery                                ' tumble random value
  theLamp = lottery // 13                       ' select a lamp, 0 - 13
  IF (theLamp = last) THEN Select_Lamp          ' no repeats
    last = theLamp

Check_Lamp:
  IF (OUTS.LOWBIT(theLamp) = 0) THEN Brighten   ' test lamp state

Dimmer:
  RANDOM lottery
  speed = lottery // 6 + 5                      ' set speed, 5 to 10
  FOR level = 255 TO 150 STEP 10                ' decrease output level
    PWM theLamp, level, 10
  NEXT
  LOW theLamp                                   ' lamp fully off
  GOTO Main2:

Brighten:
  RANDOM lottery
  speed = lottery // 6 + 5                      ' set speed, 5 to 10
  FOR level = 150 TO 255 STEP 10                ' increase output level
    PWM theLamp, level, 10
  NEXT
  HIGH theLamp                                  ' lamp fully on

IF cycleTm < 4000  THEN Backwards_Chaser:


GOTO Main2:

JonnyMac

If you post your entire program I'll sort it out for you.  Hint: You have to update the cycleTm variable after each PWM as those consume a lot of time.
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

ok here it is....


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

Trigger         PIN     15                      ' Relay closure input trigger pin

DIRS = %0111111111111111                        ' Set Pin0 to pin 14 as outputs




' -----[ Constants ]-------------------------------------------------------
INS= 15


IsOn            CON     1
IsOff           CON     0

Pressed         CON     1
NotPressed      CON     0


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

theLamp         VAR     Byte                    ' lamp to brighten or dim
last            VAR     Byte                    ' last lamp adjusted
speed           VAR     Byte                    ' adjustment speed
level           VAR     Byte                    ' output level (for PWM)
lottery         VAR     Word                    ' random value
reps            VAR     Nib                     ' FOR...NEXT loop counter
cycleTm         VAR      W4                     ' cycle timing

Main2:

Select_Lamp:
OUTS = %0111111111111110                       ' all on

cycleTm = cycleTm + 5


  RANDOM lottery                                ' tumble random value
  theLamp = lottery // 13                       ' select a lamp, 0 - 13
  IF (theLamp = last) THEN Select_Lamp          ' no repeats
    last = theLamp

Check_Lamp:
  IF (OUTS.LOWBIT(theLamp) = 0) THEN Brighten   ' test lamp state

Dimmer:
  RANDOM lottery
  speed = lottery // 6 + 5                      ' set speed, 5 to 10
  FOR level = 255 TO 150 STEP 10                ' decrease output level
    PWM theLamp, level, 10
  NEXT
  LOW theLamp                                   ' lamp fully off
  GOTO Main2:

Brighten:
  RANDOM lottery
  speed = lottery // 6 + 5                      ' set speed, 5 to 10
  FOR level = 150 TO 255 STEP 10                ' increase output level
    PWM theLamp, level, 10
  NEXT
  HIGH theLamp                                  ' lamp fully on

IF cycleTm < 4000  THEN Trigger_Relay:

Trigger_Relay:                                  'Trigger board2 for chaser sequence
Relay = IsOn
PAUSE 500
Relay = IsOff

END

JonnyMac

Okay, give the listing below a try.  Let me caution you about a couple things:

-- Keep you listing very neat an orderly; this is the best way to prevent bugs, or find them when the do pop up
-- Don't use internal variable names (B0..B25, W0..W12) in the Prop-2 -- ever.  There is no good reason and it just leads to problems

For example, you defined a variable using the internal name W4 which would have, when you added more variables, overwritten normally-declared variables; always use the VAR keyword with the variable type.  Avoid Nibs unless you really need the space -- these add a lot of internal processing as Nib is not a native variable type.

' =========================================================================
'
'   File......
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'              Copyright (c) 2007 EFX-TEK
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated...
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


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


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


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

Trigger         PIN     15                      ' Relay contacts


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

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


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

lottery         VAR     Word                    ' random value
cycleTm         VAR     Word                    ' cycle timing

theLamp         VAR     Byte                    ' lamp to brighten or dim
last            VAR     Byte                    ' last lamp adjusted
speed           VAR     Byte                    ' adjustment speed
level           VAR     Byte                    ' output level (for PWM)

reps            VAR     Byte                    ' FOR...NEXT loop counter


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

Reset:
  DIRS = %0111111111111111                      ' P0..14 are outputs



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

Main_1a:
  DEBUG CLS, "Running Main 1", CR
  cycleTm = 0

Main_1b:
  IF cycleTm >= 4000 THEN Main_2a

  RANDOM lottery                                ' tumble random value
  theLamp = lottery // 14                       ' select a lamp, 0 - 13
  IF (theLamp = last) THEN Check_Lamp           ' no repeats
    last = theLamp

Check_Lamp:
  IF (OUTS.LOWBIT(theLamp) = 0) THEN Brighten   ' test lamp state

Dimmer:
  RANDOM lottery
  speed = lottery // 6 + 5                      ' set speed, 5 to 10
  FOR level = 255 TO 150 STEP 10                ' decrease output level
    PWM theLamp, level, 10
    cycleTm = cycleTm + 10                      ' add PWM units duration
  NEXT
  LOW theLamp                                   ' lamp fully off
  GOTO Main_1b

Brighten:
  RANDOM lottery
  speed = lottery // 6 + 5                      ' set speed, 5 to 10
  FOR level = 150 TO 255 STEP 10                ' increase output level
    PWM theLamp, level, 10
    cycleTm = cycleTm + 10
  NEXT
  HIGH theLamp
  GOTO Main_1b




Main_2a:
  DEBUG "Running Main 2", CR
  cycleTm = 0


Main_2b:
  IF cycleTm >= 4000 THEN Main_3a

  cycleTm = cycleTm + 10                        ' test code only
  PAUSE 1                                       '   run 10x speed

  GOTO Main_2b




Main_3a:
  DEBUG "Running Main 3", CR
  cycleTm = 0

Main_3b:
  IF cycleTm >= 4000 THEN Main_1a

  cycleTm = cycleTm + 10                        ' test code only
  PAUSE 1                                       '   run 10x speed

  GOTO Main_3b


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


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


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


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


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


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


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