Shop OBEX P1 Docs P2 Docs Learn Events
Counting problem — Parallax Forums

Counting problem

ERMERM Posts: 34
edited 2006-03-29 23:25 in BASIC Stamp
Hi guys,

I've seen it, but I can't find it now that I need it.

I want to run a counter from 40 to 100 at 20 step intervals and back down and repeat (i.e. 40 60 80 100 80 60 40 60 80 100... and so on). I can't remember the formula for it. I know it was an easy one as well. Can anyone help please.

Thanks,
Tony

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-03-28 23:03
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    counter   VAR   byte
    FOR counter = 40 TO 100 STEP 20
      DEBUG DEC counter, cr
    next
    FOR counter = 100 TO 40 STEP 20
      DEBUG DEC counter, CR
    next
    
    

    That should work.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • ERMERM Posts: 34
    edited 2006-03-29 00:31
    Thanks Chris for your reply, but that's not what I was looking for. To go into more detail, the counter would be changing a pause rate within another loop. As the loop executes, I want the pause time to change from 40 to 100 and back. Kind of like counter = counter + 1, but when it reaches a certain number (100) I want it to reverse, something like counter = counter - 1. Any thoughts.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-03-29 01:01
    Tony,

    ·· Other than the DO...LOOP to cycle the whole thing over it is exactly what you asked for.· Without more details that's the best I can offer.· Your new details somewhat contradict the original post.· You mentioned increments of 20, but now you're mentioning increments of 1.· The DEBUG statements were there simply for filler, you could easily assign that counter to a delay or whatever.· Perhaps you should list exactly what you need to do.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-03-29 01:10
    Okay, perhaps we could try this one?
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    counter   VAR   Byte
    offset    VAR   Byte
    counter = 40
    offset = 20
    DO
      DEBUG DEC counter, CR
      PAUSE 500
      counter = counter + offset
      IF counter = 40 OR counter = 100 THEN
        offset = -offset
      ENDIF
    LOOP
    

    This one may be closer to what you wanted.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • SSteveSSteve Posts: 808
    edited 2006-03-29 01:57
    How about this? It will let you use any sequence of values you want.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    idx        VAR        NIB
    value        VAR        BYTE
    
    DO
      LOOKUP idx, [noparse][[/noparse]40, 60, 80, 100, 80, 60], value
      DEBUG "Do stuff here"
      idx = idx + 1 // 6        ' Change 6 to the number of values in your list
    LOOP
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows
  • ERMERM Posts: 34
    edited 2006-03-29 03:37
    I figured it out using the lookup command. Thanks SSteve. Thanks Chris for your input, I played with the if...then for a while and gave up on it. SSteve, I had to rewrite what you wrote because the 1 // 6 wasn't working. The new code looks like this:

    '{$STAMP BS2}

    reps VAR Nib
    rate VAR Byte
    x VAR Byte

    rate = 100

    variablespeed:

    FOR x = 0 TO 5

    FOR reps = 1 TO 2
    HIGH 1
    PAUSE rate
    LOW 1
    PAUSE rate
    NEXT

    FOR reps = 1 TO 2
    HIGH 2
    PAUSE rate
    LOW 2
    PAUSE rate
    NEXT
    PAUSE 2000
    LOOKUP x, [noparse][[/noparse]80, 60, 40, 60, 80, 100], rate

    NEXT

    GOTO variablespeed

    Thanks for the input guys, I really appreciated it.
  • SSteveSSteve Posts: 808
    edited 2006-03-29 23:25
    The mod operation happens after the addition. So when idx is 5, adding 1 makes it 6 and the "// 6" wraps it back around to 0. It works here. Try replacing the DEBUG line with
    DEBUG ? value
      PAUSE 250
    
    


    You should see the values cycle through the values in the list.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows
Sign In or Register to comment.