PDA

View Full Version : Newbie PBASIC math problem



-JR-
01-24-2007, 11:43 AM
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-

Kevin Wood
01-24-2007, 12:04 PM
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)

Fe2o3Fish
01-24-2007, 12:10 PM
Alas, logic in English ain't quite like something a computer wants. http://forums.parallax.com/images/smilies/smile.gif

Anytime you use a comparison operator you need two sides (a left side and a right side) http://forums.parallax.com/images/smilies/smile.gif.


IF (A < B) AND (A < C) THEN do something


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. http://forums.parallax.com/images/smilies/smile.gif

-Rusty- http://forums.parallax.com/images/smilies/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-
01-24-2007, 12:15 PM
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 Wood
01-24-2007, 12:47 PM
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.

allanlane5
01-24-2007, 09:10 PM
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.

Astrocreep
01-26-2007, 05:35 AM
I am new to PBASIC also. I thought there had to be an else argument with an IF...THEN statement.

Kevin Wood
01-26-2007, 05:49 AM
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 (http://www.parallax.com/html_pages/edu/downloads/downloads.asp)