November 25, 2024, 04:56:55 AM

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.


Is this the right way to use the FOR NEXT Routine

Started by samsam, March 09, 2008, 09:54:18 PM

Previous topic - Next topic

samsam

GOSUB Get_Time

DEBUG HOME, HEX2 mins, ":", HEX2 secs, CR
IF secs = $59 THEN
PAUSE 1000
ENDIF

IF chg = 0 THEN         
  PAUSE dly3
ELSE
IF chg = 1 THEN

' I want to run so many times though this routine to see if it still true if so then do something

  FOR idx = 1 TO 300

  DEBUG DEC5 ? idx , DEC5 ? leaps        ' This seem to work but is this the right way to do this

From here down i want to run so many time if

This statement stays true

IF leaps = 300 AND chg = 1 THEN EXIT     


____________________________________________________________________________________________________
  HIGH led             
LOW chg               
PAUSE dly1
           
'charge               
LOW led               
HIGH chg
PAUSE dly1

'input
LOW led
INPUT chg
PAUSE dly2             
DEBUG CR, DEC ? chg
leaps = leaps + 1

NEXT
idx = idx + 1

ENDIF
  ENDIF
  IF leaps = 300 AND chg = 1 THEN EXIT     
LOOP
Sam

I Want  Thank You for All Of Your Time And Help

...............In Helping Getting Thing To Work........

JonnyMac

Jon McPhalen
EFX-TEK Hollywood Office

samsam

I  want to run this routine 300 time and if this statement stays true then do this
IF stay true for = 300 times around  the routine chg = 1 THEN EXIT 

HIGH led             
LOW chg               
PAUSE dly1
           
'charge               
LOW led               
HIGH chg
PAUSE dly1

'input
LOW led
INPUT chg
PAUSE dly2             
DEBUG CR, DEC ? chg
leaps = leaps + 1

NEXT
idx = idx + 1

ENDIF
  ENDIF
  IF leaps = 300 AND chg = 1 THEN EXIT     
LOOP
Sam

I Want  Thank You for All Of Your Time And Help

...............In Helping Getting Thing To Work........

JonnyMac

Forgive me, Sam, I'm really struggling trying to figure out what you're after.  Please try re-stating the question, keeping things as concise as possible.  Let me help you figure out the code.
Jon McPhalen
EFX-TEK Hollywood Office

samsam

JonnyMac

Here is what i want to do is run this routine 300 times
To see if the statement that i want to use is TRUE  and STAYS TRUE until it has loop 300 tiimes

DO
HIGH led             
LOW chg               
PAUSE dly1               
LOW led               
HIGH chg
PAUSE dly1
LOW led
INPUT chg
PAUSE dly2             
DEBUG CR,
PAUSE dly3
LOOP

The statement that i want to use is this 

IF chg = 1 THEN EXIT
Sam

I Want  Thank You for All Of Your Time And Help

...............In Helping Getting Thing To Work........

JonnyMac

March 09, 2008, 11:11:33 PM #5 Last Edit: March 09, 2008, 11:14:05 PM by JonnyMac
So you want to enter an infinite loop and if a condition stays true 300 times then you exit?  That's easy; I'd do it like this:

  cntr = 0
  DO
    ' do other loop stuff
    cntr = cntr + 1 * Chg
    IF (cntr = 300) THEN EXIT
  LOOP


In order to determine that the Chg input has held a 1 state for 300 iterations of the loop you need counter (word size).  Clear this counter before the loop.  At the bottom of the loop (or wherever you want to check) you will add one to your counter and then multiply that by the Chg input (1 or 0).  So long as the input stays high the counter will keep going up; if it goes back low the equation will cause the counter to be reset to zero.

Hopefully, this helps.
Jon McPhalen
EFX-TEK Hollywood Office

samsam

JonnyMac

That is just what i was looking for

One thing

What would i have to change to have it chg = 0 and do the same thing
I do not need it now but i want to understand how to use this routine to it fullest

cntr = 0
  DO
    ' do other loop stuff
    cntr = cntr + 1 * Chg
    IF (cntr = 300) THEN EXIT
  LOOP

Thanks
Sam

I Want  Thank You for All Of Your Time And Help

...............In Helping Getting Thing To Work........

JonnyMac

You can use the inversion operator (~) to flp the state of the input:

  cntr = 0
  DO
    ' do other loop stuff
    cntr = cntr + 1 * ~Chg
    IF (cntr = 300) THEN EXIT
  LOOP


You should do some experiments with DEBUG just to verify the results.
Jon McPhalen
EFX-TEK Hollywood Office

samsam

I try ed this one but it dose not work the way the other one dose

First of all it start counting at 6550 and dose not count

cntr = 0
  DO
    ' do other loop stuff
    cntr = cntr + 1 *~ Chg
    IF (cntr = 300) THEN EXIT
  LOOP




Now i try ed this and it count but  when it changes from (CHG(0) to (1)) it dose not stop counting until it gets 300

cntr = 0
  DO
    ' do other loop stuff
    cntr = cntr + 2 ** ~Chg
    IF (cntr = 300) THEN EXIT
  LOOP

This one work just the way i want it to work when it changes from (CHG (0) to (1)) it dose stop counting and rest to 0

cntr = 0
  DO
    ' do other loop stuff
    cntr = cntr + 1 * Chg
    IF (cntr = 300) THEN EXIT
  LOOP




Sam

I Want  Thank You for All Of Your Time And Help

...............In Helping Getting Thing To Work........

JonnyMac

Be careful not to grasp at straws (trying thing willy-nilly) when working through programming problems -- that is a good way to create them.

Give this a try:

  cntr = 0
  DO
    ' do other loop stuff
    cntr = cntr + 1 * (1 - Chg)
    IF (cntr = 300) THEN EXIT
  LOOP


With the BS2 you can have parenthesis in an expression so the (1 - Chg) part inverts that value.
Jon McPhalen
EFX-TEK Hollywood Office

samsam

Jonny Mac

Be careful not to grasp at straws (trying thing willy-nilly) when working through programming problems -- that is a good way to create them.


I will keep that in mind        I still have allot of learing to  I have learned allot in the last year with your help and every one else

This works GREAT
Sam

I Want  Thank You for All Of Your Time And Help

...............In Helping Getting Thing To Work........

samsam

JonnyMac

Up Date

I am using this routine in a project that i just about done with I am just waiting for the case for this project when it done I will  post it in the Complet Project

  cntr = 0
  DO
    ' do other loop stuff
    cntr = cntr + 1 * (1 - Chg)
    IF (cntr = 300) THEN EXIT
  LOOP


I also used this routine in the project

cntr = 0
  DO
    ' do other loop stuff
    cntr = cntr + 1 * Chg
    IF (cntr = 300) THEN EXIT
  LOOP


Sam

I Want  Thank You for All Of Your Time And Help

...............In Helping Getting Thing To Work........

GOT

Jonny, I have seen you use this before and I don't see how cntr resets to 0 when Chg goes to 0 (only when cntr reaches 300).  So, if you have a noisy system, doesn't it still misfire, just 300 times less often?  Shouldn't it be

cntr = 0
  DO
    ' do other loop stuff
    cntr = cntr * (1 - Chg) +1
    IF (cntr = 300) THEN EXIT
  LOOP

or is

cntr + 1 * (1 - Chg)

the same as

(cntr + 1) * (1 - Chg) in BASIC STAMP?

JonnyMac

July 11, 2008, 01:29:25 PM #13 Last Edit: July 11, 2008, 01:31:16 PM by JonnyMac
Yes, you got it: PBASIC doesn't have operator precedence like other flavors of BASIC, so expressions are evaluated left-to-right unless you use parenthesis.  In these kinds of routines I will always do the addition to the accumulator first, then use the input multiplier (which resolves to 0 or 1) at the end of the expression.

The idea behind this code is that you need 300 straight cycles at the "active" level to move past.  Noise in a system is transient and should never last that long (if it does you have bigger problems).  A typical debounce routine for a trigger input looks like this (using PBASIC 2.5 syntax):

Main:
  timer = 0
  DO WHILE (timer < 100)
    PAUSE 5
    timer = timer + 5 * Trigger
  LOOP


Where "Trigger" is an active-high input pin.  In this case a noise pulse would have to be 100 milliseconds in duration to create a false start -- 100 milliseconds is WAY beyond system noise.
Jon McPhalen
EFX-TEK Hollywood Office

GOT

You probably just saved me from 20 hours of programming headaches.  It would have taken me a long time to figure out the precedence thing.