Shop OBEX P1 Docs P2 Docs Learn Events
Newbie PBASIC math problem — Parallax Forums

Newbie PBASIC math problem

-JR--JR- Posts: 6
edited 2007-01-25 22:49 in BASIC Stamp
I have a line that doesn't pass a syntax check using Basic Stamp Editor V2.2.6

Here it is...
· · IF (Genspeed*2) >= 1000 AND <= 1100 THEN (Throttle +(Genspeed*2 -Gensetspd))

It really doesn't like this statement.· Here is what I am trying to accomplish with this line.
If the value of·GENSPEED doubled is between 1000 and 1100 then add the difference of GENSPEED doubled and GENSETSPD to THROTTLE.

The idea here is that this line acts as a way to take my measured generator rotation speed and increase or decrease the throttle.

If anyone has any suggestions, if I'm going about this the wrong way I'd really appeciate it.· I can also supply my entire govenor program if it helps.

I am measuring a frequency that is generated by a magnetic pickup on the flywheel·and then using a high torque RC (PWM) servo to control the throttle.

THANKS!

PS - If the above line looks really funny, this is my first attempt at PBASIC or anything STAMP.
-JR-

Comments

  • Kevin WoodKevin Wood Posts: 1,266
    edited 2007-01-24 05:04
    Try something like this:

    IF ((Genspeed*2) >= 1000) AND ((Genspeed*2) <= 1100) THEN SomeVariableValue = Throttle +((Genspeed*2)-Gensetspd)

    or this:

    DblSpeed = Genspeed * 2
    IF (DblSpeed >= 1000) AND (DblSpeed <= 1100) THEN NewSpeed = Throttle +(DblSpeed-Gensetspd)
  • Fe2o3FishFe2o3Fish Posts: 170
    edited 2007-01-24 05:10
    Alas, logic in English ain't quite like something a computer wants. smile.gif

    Anytime you use a comparison operator you need two sides (a left side and a right side) smile.gif.
    IF (A < B) AND (A < C) THEN  [i]do something[/i]
    


    Oh, and to add something to a variable that already has a value, use something of this form:
    A = A + B
    


    This will add the value of B to A and put it back into A.

    So... you had
    IF (Genspeed*2) >= 1000 AND <= 1100 THEN (Throttle +(Genspeed*2 -Gensetspd))
    


    which most of us pretty much understood *BUT* you need to be a little more explicit like this:

    IF (Genspeed*2) >= 1000 AND (Genspeed*2)<= 1100 THEN Throttle = Throttle + (Genspeed*2 - Gensetspd)
    


    What follows the THEN is a statement so the parentheses around it will may the compile burp too.

    If you can spare the variable then try this so the Stamp doesn't have to do the multiply more than once:
    gen2 = Genspeed*2
    IF (gen2 >= 1000) AND (gen2 <= 1100) THEN Throttle = Throttle + (gen2 - Gensetspd)
    
    



    That's how the syntax works! Now, if that arithmetic isn't REALLY what you want.... someone else will have
    to help you there. smile.gif

    -Rusty- tongue.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Rusty-
    --
    Rusty Haddock = KD4WLZ = rusty@fe2o3.lonestar.org
    **Out yonder in the Van Alstyne (TX) Metropolitan Area**
    Microsoft is to software what McDonalds is to gourmet cooking
  • -JR--JR- Posts: 6
    edited 2007-01-24 05:15
    Thanks so much guys!

    While I have your attention...
    is it possible to preform two functions from a If..Then statement?

    For example...
    IF INPUT1= 1 THEN LOW OUTPUT2 AND HIGH OUPUT3

    This doesn't work, but I can't find an example of how to accompish this without more lines.
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2007-01-24 05:47
    You can do multiple operations, but not like that. AND is a comparison operator only, not a directive.

    There are a few ways to do what you want.

    One is IF..THEN..ELSE:

    IF SomeCondition THEN
    HIGH X
    HIGH Y
    ELSE
    LOW X
    LOW Y

    Another is SELECT..CASE:

    SELECT SomeVariable
    CASE X
    HIGH A
    HIGH B
    CASE Y
    HIGH A
    LOW B
    CASE Z
    LOW A
    LOW B
    ENDSELECT

    Combining either of the above methods with GOSUB subroutine calls can make your code more readable and modular:

    IF SomeCondition THEN
    GOSUB LedsOn
    ELSE
    GOSUB LedsOff

    or...

    SELECT SomeVariable
    CASE X
    GOSUB LedsOn
    CASE Y
    GOSUB LedsOff
    CASE Z
    GOSUB SomeOtherTask
    ENDSELECT

    calls these subroutines:

    LedsOn:
    HIGH X
    HIGH Y
    RETURN

    LedsOff:
    LOW X
    LOW Y
    RETURN

    SomeOtherTask:
    GOSUB ReadFromSerialInput
    GOSUB ProcessTheInput
    GOSUB WriteToSerialOutput
    GOSUB etc...
    RETURN

    The idea being to write small chunks of code that you can use as building blocks for your program.
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-01-24 14:10
    This construct works:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    Input1 VAR Word
    Output2 CON 2
    OUTPUT3 CON 3


    IF INPUT1= 1 THEN : LOW OUTPUT2 : HIGH OUTPUT3 : ENDIF

    This takes advantage of the multi-line "IF" statement -- each ":" character
    to PBasic says "treat this as another line". It's EXACTLY the same as:

    IF Input1 = 1 THEN
    LOW OUTPUT2
    HIGHT OUTPUT3
    ENDIF

    and will tokenize the same for the BS2. But if for some formatting reason you REALLY
    want it all on one line, the above will work.
  • AstrocreepAstrocreep Posts: 2
    edited 2007-01-25 22:35
    I am new to PBASIC also. I thought there had to be an else argument with an IF...THEN statement.
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2007-01-25 22:49
    IF..THEN and IF..THEN..ELSE are two different constructs. You can see code samples in the PBasic help file from the Stamp editor.

    Essentially, they are both just ways to help manage program flow. You can use the one which is most applicable to a given problem.

    If you are interested in reading more about program flow in PBasic, check out the Parallax BASIC Stamp Tutorial on this page:

    www.parallax.com/html_pages/edu/downloads/downloads.asp
Sign In or Register to comment.