Macro Math Discussion
Home ] Up ] Basic Concepts ] Serial Connection ] Plotting Basics ] Logging, Saving and Printing ] Interactive Control ] Macro Basics ] Macros - Default & Events ] [ Macro Math Discussion ] Defined Macro Buttons/Menus ] Drawing Basics ] Media and Paths ] Drawing Coordinate Modes ] Plot Objects ] Macro Editor ] Using Templates ] External Applications ] Historical/Logged Data ] Internet ] Binary and Special Data ]

 

NEW MATH! (Release 7.0)
bullet

StampPlot can now perform multiple expressions in a single line.

bullet

Operations are from left to right.

bullet

Spaces MUST be used between each value/operation.

bullet

Operations are performed left to right, unless additional brackets are used.

bullet

If you distribute your macros and use "new math", ensure users upgrade their product.

bullet

See release updates

With Macro Math, the following are possible:

bulletData markers
bulletUsing !MATH
bulletUse brackets [ ] to perform calculations as part of instructions or text.
bulletUsing Macro Values
bulletBoolean Math
bulletAssign names to data markers.
bulletUsing marker pointers.
bulletSimple For-Loop
bulletPerforming conditional evaluations
bulletActing as a Math Co-Processor

bullet Data Markers

Their exists 100 data markers which may be used to hold variable information.  Data markers are named %m0 to %m99.  Data markers are of the variant data type meaning they may hold strings, values, or dates and times.

The data markers can be used be using in instructions or text. Their values will be inserted during processing.

For example:
!STAT Data marker 1=%m0

Note: If Use Analog data for macro is enabled (!USEA ON), the analog values separated by a comma will automatically be assigned to to data markers beginning at %m0.

Data markers are cleared on a New Plot (!NEWP).

bullet!MATH

The !MATH instruction can be used in a macro to perform math operations.  The math operation may be assignment or a calculation.  The typical format of a math instruction is:
!MATH marker=value1,operation,value2
!MATH %m10=10,*,5
!STAT %m10

Some math operations perform calculations using a single value, or even no value:
!MATH marker=value1,operation
'Stores random number in %m10, converts to digital and stores in %m11
'Enter lines individually in immediate window

!MATH %m10=RND
!MATH %m11=%m10,ADC
!STAT %m10 is %m11 in binary

Only one operation may be performed per MATH instruction, though later we will discuss using brackets for multiple operations.  Multiple MATH operations should not be separated by soft returns (CR).

Values may be assigned using:
!MATH marker=value
!MATH %m10=Hello
!STAT %m10

There is a wide assortment of math operations for use.  Please see the Math Instruction Summary.

bulletUsing Math Brackets

By using brackets, [ ], math may be formed within other code and text simplifying use.  Each bracket-pair is treated as a MATH instruction.  Pairs may be nested with precedence of inside-out.

!STAT [10,*,5]
!STAT The SIN of 45 is [45,SIN]
!STAT [[[45,SIN],*,10],Format,0.00]
!STAT [[10,*,5],+,[10,/,5]]

bulletUsing Macro Values

Macro Values are plot and object values which may be used in instructions.  They are identified by placing keywords within parenthesis ( ).  
'Shows current time + 1 hour
!STAT The time in one hour will be [(RTIME),+,1:00]

There exists a wide assortment of Macro Values.  Please see the Math Instruction Summary for a full listing.

bulletBoolean Math

Boolean math is a special category.  It may be used to perform the operations of AND, OR, NOT and XOR on 2 values, or in the case of NOT, on 1 value.
NOT:   [bit']  [0']
AND:   [bit*bit]  [1*0]
OR:     [bit+bit] [1+1]
XOR:   [bit^bit]  [1^0]

!STAT 0 NOT is  [0']: 0 OR 1 is [0+1]: 0 AND 1 is [1*0]: 0 XOR is [1^1]

Note:  Only 2 values per operation, NO SPACES.  Also note there are no commas within it.

For example, to AND 3 values together:
!STAT [[1*1]*0]

Once you start working with data and objects, a use could be:
!POBJ imgBut=[(BIT1)*(chkHigh)]

bulletAssigning names to Data Markers

Using the !MVAR instruction, names may be assigned to data markers, and optionally assigned values.  That value may then be used in text or instructions where it it replaced.
!MVAR name
!MVAR name=value

!MVAR V_number=100
!STAT V_number, [V_number,+,500]

The first variable name is assigned to %m0, the second to %m1 and so on.  Repeated !MVAR calls for the same name will assign the name to multiple markers (not recommended).

To clear out the variable names, use !CVAR (clear variables).

Then using analog data for macro only (!USEA ON), incoming data is automatically assigned to to markers beginning at %m0.  For this reason, you should assign as many markers as comma-separated values you expect to be receiving if you use names and will be using analog data only for macros, not for plotting directly.

Simple simple search and replace is used, the variable names should be unique.  They ARE case-sensitive, and are replaced in reverse order (%m99 to %m0).

The use of variable names is not as important as it was prior to version 3.

bulletData Marker Pointer

The data marker pointer can be used to address a marker by setting a pointer for the marker location.

To set the pointer:
!PNTR 5
Sets the pointer to be 5

!MATH %m&=10
Sets %m5 to be 10

!STAT %m&
Displays %m5

bulletSimple For-Loop

The !FORP (FOR pointer) can be used as simple for-loop with value substitution.
!FORP val1,val1,code
!FORP 0,10,!DBUG &(CR)!DBUG &*10 = [&,*,10]
Will display the values, and the values 0 to 10 where & is replaced by the current count.
Reverse counting is allowed. Note that a soft return (CR) can be used for multiple statements.

bulletConditional Evaluation

!IFTH (If-Then) can be used to evaluate 2 expressions, and if true, execute the code.
!IFTH val1,condition,val2,code statement or statements separated by (CR)

!IFTH 15,>,10,!STAT Higher
!IFTH (AINVAL0),>,100,!BELL

For values or times, conditionals use may be:  = (or ==), <,>,! (or <>), >=, <=
In addition to the previous, string may also use:
~ similar: Non-Case sensitive comparison
@ in-string: string 1 contains string 2 - Case sensitive
!IFTH Hello,@,lo,!STAT In string!

Conditionals can also be used in bracket expressions to return a 1 (true) or 0 (false).  Do not use = for evaluating equality, use == instead.

!STAT [hello,~,HELLO]

Conditionals in strings
Conditional evaluations may be performed in strings, and return a 0 is false and 1 if true.
[conditional1,equality,conditional2]

!STAT [(RTIME),>,14:00]

Equalities:
==   Equal (= will NOT work)
>     Greater than
>=   Greater then or equal to
<     Less than
<=   Less than or equal to
<> or !   Not equal to

For strings only:
~     Is similar to- case insensitive comparison
@    Contains- Conditional1 contains conditional2  - Case Sensitive

The equalities can be used for Boolean math for AND and OR operations.
!STAT [[(RTIME),>,13:00]*[(RTIME),<,14:00]]
(returns 1 if time is between 13:00 and 14:00)

If these are used within an If-Then, the If-Then equality is still required. 
!IFTH [[(RTIME),>,13:00]*[(RTIME),<,14:00]],=,1,!BELL

bulletActing as a math co-processor:

One other trick is to use StampPlot as a math co-process for your micro.
When sending data for plotting, for example:
!DEBUG "[[", DEC X, ",*,10],+,5]",CR
Will have the Basic Stamp send the string (assuming x is 5):
[[5,*,10],+,5]
Which will cause StampPlot to evaluate the expression prior to plotting it at the value of 55.

We can also have StampPlot send back the results of an operation:
!DEBUG "!READ [[[", DEC X, ",SIN],*,100],INT]",CR
Which will send the string (assuming X is 45):
!READ [[[45,SIN],*,100],INT]
StampPlot will evaluate the expression to 70 and return the value serially to the Basic Stamp to be caught by a SERIN instruction and used by the Stamp.