Shop OBEX P1 Docs P2 Docs Learn Events
Newbie to Stamps - code question — Parallax Forums

Newbie to Stamps - code question

RonMRonM Posts: 8
edited 2007-03-15 23:36 in Learn with BlocklyProp
I'm going thru the WAM book with the BOE USB board and a BS2 revC. I'm using PBasic 2.5. Servo examples but the issue I have is not with the servo.

When I try to execute a For loop using a step of -1, it does not seem to work as I expect. What appears to happen is:
a. It steps forward by 1 and then
b. drops thru

So if I have :

i VAR WORD

FOR i = 1000 TO 500 STEP -1
·· ' do something
NEXT

It executes what is in the for loop once but the value of "I" when I debug it is 1001.

PS. Software developer by trade.

Thanks,
Ron
·

Comments

  • Steph LindsaySteph Lindsay Posts: 767
    edited 2007-03-15 20:45
    Hi RonM,

    Here is an explanation of valid StepValue arguments from the FOR...NEXT entry of the Help file.· It looks like the minus sign is only valid with the BS1,and not needed for the BS2 models; just having your StarValue greater than your EndValue is sufficient:

    [*]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.

    Hope you are having fun with WAM.

    -Stephanie Lindsay,

    Editor, Parallax Inc.
  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-15 20:48
    If you read the description of the FOR command, you'll see that, for all BS2 Stamps, the interpreter determines the direction from the relative values of the start and end values and the step value should always be positive. That's why you get a value for I of 1001. The interpreter noted that the starting value was higher than the ending value and subtracted the step (-1).
  • RonMRonM Posts: 8
    edited 2007-03-15 20:51
    Thanks Stephanie and Mike.
  • RonMRonM Posts: 8
    edited 2007-03-15 21:07
    Well, just for my clarification. Now that I've read the Help on the command, I would guess the following:

    Using BS1 we could step using a negative step (-1, -2, -3, -4, etc) . But any Stamp after the BS1 (I'm new so bear with me on this), a negative step is assumed at it can only be by "-1". There is no way of stepping -4. Sorry to be a pain, I'm a software guy and once I get my hands around the syntax, I'll hit my comfort level.
  • FranklinFranklin Posts: 4,747
    edited 2007-03-15 21:21
    My understanding is that if the start number is bigger than the end the step will be counting down and will be in the increment you specify
    For i =1000 to 500 step 5
    will count down from 1000 in steps of 5 to 500.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • Steph LindsaySteph Lindsay Posts: 767
    edited 2007-03-15 23:36
    Franklin is correct - for the BS2 the StepValue can be a variable, constant or expression, in the range of·0-65535, and to·to count down instead of count up,·just make your StartValue greater than your EndValue.

    -Stephanie
Sign In or Register to comment.