Newbie PBASIC math problem
-JR-
Posts: 6
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-
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
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)
Anytime you use a comparison operator you need two sides (a left side and a right side) .
Oh, and to add something to a variable that already has a value, use something of this form:
This will add the value of B to A and put it back into A.
So... you had
which most of us pretty much understood *BUT* you need to be a little more explicit like this:
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:
That's how the syntax works! Now, if that arithmetic isn't REALLY what you want.... someone else will have
to help you there.
-Rusty-
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
-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
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.
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.
' {$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.
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