Shop OBEX P1 Docs P2 Docs Learn Events
For loop question — Parallax Forums

For loop question

Nick FoxNick Fox Posts: 4
edited 2005-11-01 17:26 in BASIC Stamp
I have the following for loop but it doesnt seem to increment, the debug statement just displays 0 and then stops. is there something wrong there?

thanks
Nick


FOR speed = 0 TO -250 STEP -10
DEBUG SDEC speed, CR
PWM_REG1 = speed
PWM_REG2 = speed
GOSUB SETDC
NEXT

Comments

  • Bruce BatesBruce Bates Posts: 3,045
    edited 2005-11-01 06:42
    Nick -

    The answer to your question will probably only be revealed if we see what's in the SETDC routine (ergo see the whole program). If there's no RETURN statement in it, that's your answer.

    I suppose I should add for completeness, that·your FOR ... NEXT·routine will NEVER increment, it will only DECREMENT, for fairly obvious reasons.

    Regards,

    Bruce Bates

    Post Edited (Bruce Bates) : 11/1/2005 6:45:53 AM GMT
  • Nick FoxNick Fox Posts: 4
    edited 2005-11-01 06:51
    Thanks Bruce

    Yes, I should have said decrement. Here is SetDC. I don't think it should have any effect on the for loop.

    SETDC:
    CMMD = $D0 'SETDC command
    ADDR = $01 'MMC default address of "1"
    LENG = $04 'Length of SETDC is 4
    CKSUM = CMMD+ADDR+LENG+P1HI+P1LO+P2HI+P2LO
    SEROUT FM,BAUD,[noparse][[/noparse]CMMD,ADDR,LENG,P1HI,P1LO,P2HI,P2LO,CKSUM]
    RETURN
    END:
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2005-11-01 07:08
    Nick -

    Make the beginning of the program look like that below, and try it again:

    DEBUG "Starting"

    FOR speed = 0 TO -250 STEP -10

    ...

    Etc

    If you see the word "Starting" more than once in any single iteration of the program, the Stamp is being reset. Therein, something is probably drawing down the power supply that is providing power to the Stamp.

    Often, if the power source is powering both the Stamp, and some external device, such as a motor or relay, this can happen easily. At that juncture you will need either a beefier power supply, or two power supplies; one for the external device, and one for the Stamp. If you go the latter route, make sure the GROUNDS are COMMON at some point in the overall circuitry.

    Regards,

    Bruce Bates
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-11-01 15:51
    Actually, you cannot count like that in your FOR...NEXT loop.· You cannot have a negative STEP value and I don't believe you can specify a negative number in that manner within the FOR...NEXT routine.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • SPENCESPENCE Posts: 204
    edited 2005-11-01 16:01
    CHRIS,

    MIGHT TRY

    FOR SPEED = 250 TO 0 STEP -10

    REF STAMP WORKS BASIC STAMP PROGRAMMING MANUAL 2.0b PAGE 118-119

    SPENCE

    Post Edited (SPENCE) : 11/1/2005 4:04:34 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-11-01 16:13
    SPENCE,

    ·· THAT WON'T WORK UNLESS YOU REMOVE THE NEGATIVE SYMBOL FROM THE STEP VALUE.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2005-11-01 16:48
    Gents -

    Since decrementing the FOR ... NEXT index or counter is perfectly legitimate, the following from the HELP FILE may provide a better understanding of how to accomplish it:

    quote

    FOR Counter = StartValue TO EndValue {STEP StepValue} ... NEXT

    StepValue is an optional variable/constant/expression (0 - 65535) by which the Counter increases or decreases with each iteration through the FOR...NEXT loop. On the BS1, use a minus sign (-) in front of the StepValue to indicate a negative step. On all other BASIC Stamps, if StartValue is larger than EndValue, PBASIC understands StepValue to be negative, even though no minus sign is used.

    end quote

    Regards,

    Bruce Bates
  • Nick FoxNick Fox Posts: 4
    edited 2005-11-01 17:26
    I ended up using the bit of code below which works nicely and allowed me to get rid of the speed variable. Thanks for your help.

    Nick

    CASE "b"
    DEBUG CR, "Back", CR

    IF PWM_REG1 = -250 THEN GOTO ExitB ' in case someone hits back twice
    Ramp_Down:
    PAUSE 10
    PWM_REG1 = PWM_REG1 - 10
    PWM_REG2 = PWM_REG2 - 10
    GOSUB SETDC
    IF PWM_REG1 <> -250 THEN Ramp_Down
    ExitB:
Sign In or Register to comment.