Skip to main content

Macro B Reference Guide

__Use__MachMotion_Logo_HiRes_TransBG-update-01-450.png

Macro B programming may not be included on your MachMotion control.  If you are not able to use these macros, contact us for an upgrade option.

1. Calling macros

Call Formats Example Notes License Required
M98 P__ Q__ L__
M98 P0100 Q5 L2


Run subroutine 0100 twice, starting at line 5 (N5)

M98 calls the subroutine specified by P (must be 4 digits), and starts at the line number specified by Q (optional). It will repeat it the number of times passed to L (default is 1) Any
G65 P__ L__ A__ B__ ….
G65 P0150 L2 A5 B10


Run macro 0150 twice, passing 5 into #1 (A) and 10 into #2 (B)

M65 calls a non-modal macro specified by P and passes A-Z (whichever are specified) to the macro. M65 repeats a number of times set by L and does not need to be cancelled. Pound variables A-Z are wiped after the macro completes. Industrial

G66 P__ A__ B__ ….

G66 P1001 A12 B6
A10 B5
G67

Run macro 1001 with A = 12 and B = 6, then run it again with A = 10 and B = 5, then cancel the modal.

G66 is a modal macro call. It is cancelled by G67. Any lines in between will call the same macro with whichever parameters you pass it.

Industrial

G67

G67

Cancels the modal G66

Industrial

M99

M99

Specifies end of subroutine or macro

Any

2. Passing Variables

You can pass variables to macros using letters A-Z, which correspond to pound variables. These are not places to store important information! Once you leave a function it is unknown what will be in that pound variable. The letters G, L, N, O, and P cannot be used as variables (because they are used as parameters for G65 or M98 or have a special syntactic use, i.e. N for line numbers or G for G codes).

Example:

G65 P0100 A5        (Invoke the macro 100, pass in the value of 5 for A.)
M30                 (Rewind the file.)

O0100               (Start of macro.)
#500 = #1           (The value of the A parameter, stored in #1, is stored in #500.)
M99                 (Return from the macro.)
%

2.1 (Local) Variables

Alphabetic Order

A

B

C

D

E

F

H

I

J

K

M

Q

R

S

T

U

V

W

X

Y

Z

1

2

3

7

8

9

11

4

5

6

13

17

18

19

20

21

22

23

24

25

26

Numeric Order
A B C I J K D E F H M Q R S T U V W X Y Z
1 2 3 4 5 6 7 8 9 11 13 17 18 19 20 21 22 23 24 25 26

3. In a macro

3.1 Storage Variables

There are two ranges of pound variables for the user to store values in.

  1. #100-#199 : Values will be cleared every time Mach starts up
  2. #500-#599 : Values will be saved every time Mach shuts down

3.2 Arithmetic

There are the basic add/subtract/multiply/divide functions available to the user, as well as some named functions for more advanced math. When performing arithmetic, the control follows the order of [Brackets, Functions, Multiply, Divide, Add, Subtract].

Function

Symbol

Example

Notes

Assignment

=

#100 = 5

Sets #100 equal to 5

Addition

+

#100 = 2 + 3

Sets #100 equal to the value of 2+3 (5)

Subtraction

-

#100 = 2 – 3

Sets #100 equal to the value of 2-3 (-1)

Multiplication

*

#100 = 2 * 3

Sets #100 equal to the value of 2*3 (6)

Division

/

#100 = 2 / 3

Sets #100 equal to the value of 2/3 (0.66)

Absolute Value

ABS[ ]

#100 = ABS[ -3 ]

Sets #100 equal to the absolute value of -3 (3)

Rounding

ROUND[ ]

#100 = ROUND[ 3 / 2 ]

Sets #100 equal to the rounded value of 3/2 (2)

Round Down (floor)

FIX[ ]

#100 = FIX[ 3 / 2 ]

Sets #100 equal to the floor of 3/2 (1)

Round Up (ceiling)

FUP[ ]

#100 = FUP[ 3 / 2 ]

Sets #100 equal to the ceiling of 3/2 (2)

Sine

SIN[ ]

#100 = SIN[ 45 ]

Sets #100 equal to the sine of the value (0.707). Takes degrees.

Arcsine

ASIN[]

#100 = ASIN[0.5]

Sets #100 equal to the sin‑1(0.5). The return value is in degrees.

Cosine

COS[ ]

#100 = COS[ 45 ]

Sets #100 equal to the cosine of the value (0.707). Takes degrees.

Arccosine

ACOS[ ]

#100 = ACOS[ 0.707 ]

Sets #100 equal to the arc cosine of the value in degrees (45)

Tangent

TAN[ ]

#100 = TAN[ 45 ]

Sets #100 equal to the tangent of the value (1). Takes degrees.

Arctangent

ATAN[ ]

#100 = ATAN[ 1 ]

Sets #100 equal to the arc tangent of the value in degrees (45)

Square Root

SQRT[ ]

#100 = SQRT[ 9 ]

Sets #100 equal to the square root of the value (3)

Natural Logarithm

LN[ ]

#100 = LN[ 2.3 ]

Sets #100 equal to the natural logarithm of the value (0.83)

Exponent

EXP[ ]

#100 = EXP[ 3 ]

Sets #100 equal to the constant e raised to the value passed

Modulus

MOD

#100 = 150 MOD 7

Sets #100 equal to the remainder of 150 ÷ 7.

(Logical/Bitwise) And

AND

#3003 AND 2

Returns the 2nd LSB (least significant bit) from the current value of #3003. (E.g. 7 AND 2 → 2 and 5 AND 2 → 0)

(Logical/Bitwise) OR

OR

#3003 OR 2

Returns the current value of #3003 with the 2nd LSB set to 1. (E.g. 1 OR 2 → 3 and 6 OR 2 → 6)

(Logical/Bitwise) XOR (exclusive OR)

XOR

#3003 XOR 2

Returns the current value of #3003 with the state of the 2nd LSB toggled. (E.g. 7 XOR 2 → 5 and 9 XOR 2 → 9)

Conversion from BCD to BIN

BIN[]



Conversion from BIN to BCD

BCD[]



Read Machine Parameter Value

PRM[<prm>]

#100 = PRM[1916]

Sets #100 equal to the value of parameter #1916 (ODID Dresser Location)

Read Machine Parameter Value Bit

PRM[<prm>,<bit>]

#100 = PRM[6001,7]

Sets #100 equal to the state of bit 7 (0x40) in PV_CM2 (#6001): whether #1-#33 get cleared on reset.



3.3 Logic Statements

Operator GOTO, IF, and WHILE are used for decision making within a macro. Be careful not to create infinite loops.

Operator

Example

Notes

N

N5

Labels the line with a number, which can be referenced on other lines.

GOTO

GOTO 5

Skip to line 5 (line labeled N5). Can be above or below current line.

IF

IF [condition] GOTO 5

#100 = 1

GOTO 6

N5 #100 = 0

N6 #500 = #100

If the condition is true, it will execute the statement on that line. If it is false, it will skip it. It is combined with GOTO to create true and false sections of code.

WHILE

WHILE [condition] DO 1

Statements

END 1

While the condition is true, the code will loop through the code between DO 1 and END 1. The numbers 1 correspond to each other. It can be any number as long as it is the same at both the start and the end.

3.4 Logic Operators

These operators are used inside of IF/WHILE conditions

Operator

Example

Notes

EQ

IF [ #500 EQ 1] GOTO 5

Equal To

NE

IF [ #500 NE 1] GOTO 5

Not Equal

LT

IF [ #500 LT 1] GOTO 5

Less Than

GT

IF [ #500 GT 1] GOTO 5

Greater Than

LE

IF [ #500 LE 1] GOTO 5

Less Than or Equal To

GE

IF [ #500 GE 1] GOTO 5

Greater Than or Equal To

3.5 Auxiliary Functions

There are additional pound variables which have functionality that may be useful to the user.



Pound Variable

Functionality

Example

#0

NIL

IF [#500 = #0] THEN M99

#1000-#1031

Input Signals 0-31 (read only). #1032 reads all 32 inputs as one value.


#1100-#1131

Output Signals 0-31 (read/write). #1132 reads or writes all 32 outputs as one value.


#3001

Millisecond timer. Setting it to 0 will reset the timer, and its value at any point can be stored in another pound variable.

(This function waits 1 second)

#3001 = 0

WHILE [#3001 LE 1000] DO 1

END 1

#3002

Hour timer. Setting it to 0 will reset the timer, and its value at any point can be stored in another pound variable.

#100 = #3002

WHILE[condition] DO 1

Statements

END 1

#101 = #3002 - #100

(#101 now holds the difference in hours to run the while loop)

#3000

Generates alarm. Gcode will be stop and the operator must reset to continue.

#3000 = 1 (Example Message)

#3006

Generates an alarm with an associated message. Requires a reset before continuing.

#3006 = 101 (Example Message)

#3007

Reports the state of Mirror Image, cannot be set (use G50 or G51 to set)

#100 = #3007

#3004

Bit flags: add 1 to disable feed hold, 2 to disable feedrate override, 4 to enable exact stop.

#3004 = 3

(disable feed hold and feedrate override, exact stop off)

#3005

Settings data. Bit 2 (value 4) is set for imperial units, clear for metric.

#100 = #3005 AND 4

(#100 is 4 in imperial, 0 in metric)

#3011

Returns the current data in YYYYMMDD form

#100 = #3011 (get date)

#100 = FIX[ #100 / 10000]

(#100 now has just the year)

#3012

Returns the current time in HHMMSS form

#100 = #3012

#3100

Returns nil

#100 = #3100 (clear #100)

#3101

Returns pi

#100 = #3101 * [3 * 3]

(area of a circle = pi * r-squared)

#3102

Returns the constant e

#100 = #3102

#6000

Set bit b00X00000 to 1 to step into a subroutine or macro. Set it to 0 step over it. 

For example, use this gcode to step into a subroutine:

G10 L50
N6000 R00100000
G11


3.6 System Variables

It is not recommended that you change the values of these pound variables, though they are available to the user. Changing values can result in unexpected machine movement.

When setting modal groups, multiply the desired value by ten. For instance, to set the active motion g-code (#4001) to a feed rate move (G01), set #4001 = 10.

Note: In October 2026 several Mach variables moved so they no longer overlap the Fanuc 31i layout. Programs that use the old numbers must be updated. #5081-#5100 and #5181-#5200 are now read only, and writing to them raises alarm PS0116.

Moved Variables

Old

New

G92 offsets

#5030-#5035

#5161-#5166

G92.3 saved offsets

#5050-#5055

#5171-#5176

G31 probe machine position

#5071-#5076

#5391-#5396

Head shift

#5081-#5086

#5381-#5386

G76 values (lathe)

#5140-#5145

#5150-#5155

G30 P1 position

#5181-#5186

#5341-#5346

G60 approach distances

#5440-#5445

#5451-#5456

G54.1 P49 and up

#7961-#8986

#14001+ (mill), #30001+ (lathe)

Modal Groups

 

Pound Variable

Description

#4001

Active motion G-code

#4002

Active motion plane

#4003

Motion mode (incremental or absolute)

#4004

Arc center mode

#4005

G93 (inverse time) or G94 unit/min

#4006

Imperial or metric units

#4007

Current cutter compensation mode

#4008

Tool length offset

#4009

Canned cycles

#4010

Used for canned cycles, old z or r-plane

#4011

Scale mode

#4012

Modal macro

#4013

Spindle mode

#4014

Coordinate systems

#4015

Exact path or cutting mode

#4016

Coordinate system rotation mode

#4017

Polar mode

#4018

Compensation mode



Buffered Values


Pound Variable

Description

#4102

Last buffered B code value

#4107

Last buffered D code value

#4108

Last buffered E code value

#4109

Last buffered F code value

#4111

Last buffered H code value

#4113

Last buffered M code value

#4114

Last buffered N code value

#4115

Last buffered O code value

#4119

Last buffered S code value

#4120

Last buffered T code value



Original Offsets


Pound Variable

Description

#4140

Original X Offset

#4141

Original Y Offset

#4142

Original Z Offset

#4143

Original A Offset

#4144

Original B Offset

#4145

Original C Offset



Last Outputs


Pound Variable

Description

#5001

Last X Output

#5002

Last Y Output

#5003

Last Z Output

#5004

Last A Output

#5005

Last B Output

#5006

Last C Output



Machine Positions


Pound Variable

Description

#5021

Current Mach X Position

#5022

Current Mach Y Position

#5023

Current Mach Z Position

#5024

Current Mach A Position

#5025

Current Mach B Position

#5026

Current Mach C Position



G92 Offsets


Pound Variable

Description

#5161

G92 X Offset

#5162

G92 Y Offset

#5163

G92 Z Offset

#5164

G92 A Offset

#5165

G92 B Offset

#5166

G92 C Offset



Part Positions


Pound Variable

Description

#5041

Current X Part Position

#5042

Current Y Part Position

#5043

Current Z Part Position

#5044

Current A Part Position

#5045

Current B Part Position

#5046

Current C Part Position



G92.3 Saved Offsets


Pound Variable

Description

#5171

Saved X Offset (restored by G92.3)

#5172

Saved Y Offset (restored by G92.3)

#5173

Saved Z Offset (restored by G92.3)

#5174

Saved A Offset (restored by G92.3)

#5175

Saved B Offset (restored by G92.3)

#5176

Saved C Offset (restored by G92.3)



G31 Positions


Pound Variable

Description

#5061

Probe Part X Coordinate

#5062

Probe Part Y Coordinate

#5063

Probe Part Z Coordinate

#5064

Probe Part A Coordinate

#5065

Probe Part B Coordinate

#5066

Probe Part C Coordinate

#5391

Probe Machine X Coordinate

#5392

Probe Machine Y Coordinate

#5393

Probe Machine Z Coordinate

#5394

Probe Machine A Coordinate

#5395

Probe Machine B Coordinate

#5396

Probe Machine C Coordinate



Tool Offset in Effect (read only)


Pound Variable

Description

#5081-#5100

Tool offset in effect for axes 1-20. Mill: tool length compensation. Lathe: tool wear offset (#5081 X, #5082 Z, #5083 Y).



Head Shift Offsets


Pound Variable

Description

#5381

X Position Head Shift

#5382

Y Position Head Shift

#5383

Z Position Head Shift

#5384

A Position Head Shift

#5385

B Position Head Shift

#5386

C Position Head Shift



G76 Values (Lathe Only)

 

Pound Variable

Description

#5150

Minimum pass depth

#5151

Finish pass depth

#5152

Number of finish passes

#5153

Thread angle

#5154

Chamfer amount

#5155

Cutting method



Cutter Compensation


Pound Variable

Description

#5157

Cutter compensation X value

#5158

Cutter compensation Y value

#5159

Cutter compensation Z value



Distance to Go (read only)


Pound Variable

Description

#5181-#5200

Distance to go for axes 1-20



G30 P1 Coordinates


Pound Variable

Description

#5341

G30 P1 X Coordinate

#5342

G30 P1 Y Coordinate

#5343

G30 P1 Z Coordinate

#5344

G30 P1 A Coordinate

#5345

G30 P1 B Coordinate

#5346

G30 P1 C Coordinate



G30 P2 Coordinates


Pound Variable

Description

#5351

G30 P2 X Coordinate

#5352

G30 P2 Y Coordinate

#5353

G30 P2 Z Coordinate

#5354

G30 P2 A Coordinate

#5355

G30 P2 B Coordinate

#5356

G30 P2 C Coordinate



G30 P3 Coordinates


Pound Variable

Description

#5361

G30 P3 X Coordinate

#5362

G30 P3 Y Coordinate

#5363

G30 P3 Z Coordinate

#5364

G30 P3 A Coordinate

#5365

G30 P3 B Coordinate

#5366

G30 P3 C Coordinate



G30 P4 Coordinates


Pound Variable

Description

#5371

G30 P4 X Coordinate

#5372

G30 P4 Y Coordinate

#5373

G30 P4 Z Coordinate

#5374

G30 P4 A Coordinate

#5375

G30 P4 B Coordinate

#5376

G30 P4 C Coordinate



Work Shift Offsets


Pound Variable

Description

#5201

X Work Shift

#5202

Y Work Shift

#5203

Z Work Shift

#5204

A Work Shift

#5205

B Work Shift

#5206

C Work Shift



G60 Approach Distances


Pound Variable

Description

#5451

G60 X Distance

#5452

G60 Y Distance

#5453

G60 Z Distance

#5454

G60 A Distance

#5455

G60 B Distance

#5456

G60 C Distance





G54 Fixture Offsets


Pound Variable

Description

#5221

X Offset

#5222

Y Offset

#5223

Z Offset

#5224

A Offset

#5225

B Offset

#5226

C Offset



G55 Fixture Offsets


Pound Variable

Description

#5241

X Offset

#5242

Y Offset

#5243

Z Offset

#5244

A Offset

#5245

B Offset

#5246

C Offset



G56 Fixture Offsets


Pound Variable

Description

#5261

X Offset

#5262

Y Offset

#5263

Z Offset

#5264

A Offset

#5265

B Offset

#5266

C Offset



G57 Fixture Offsets


Pound Variable

Description

#5281

X Offset

#5282

Y Offset

#5283

Z Offset

#5284

A Offset

#5285

B Offset

#5286

C Offset



G58 Fixture Offsets


Pound Variable

Description

#5301

X Offset

#5302

Y Offset

#5303

Z Offset

#5304

A Offset

#5305

B Offset

#5306

C Offset



G59 Fixture Offsets


Pound Variable

Description

#5321

X Offset

#5322

Y Offset

#5323

Z Offset

#5324

A Offset

#5325

B Offset

#5326

C Offset



G54.1 P1


Pound Variable

Description

#7001

X Offset

#7002

Y Offset

#7003

Z Offset

#7004

A Offset

#7005

B Offset

#7006

C Offset



G54.1 P2


Pound Variable

Description

#7021

X Offset

#7022

Y Offset

#7023

Z Offset

#7024

A Offset

#7025

B Offset

#7026

C Offset



G54.1 P1-P300


Pound Variable

Description

#7001 + 20 × (P - 1)

G54.1 P1 through P48 X Offset (Y through C follow)

#14001 + 20 × (P - 1)

Mill: G54.1 P1 through P300 X Offset

#30001 + 20 × (P - 1)

Lathe: G54.1 P1 through P300 X Offset (#7001-#7960 and the mill or lathe range are two names for the same offsets)


3.7 Tool Variables

Mill Tool Offsets

Pound Variable

Description

#10001-#10999

Tool 1-999 height (H) wear

#11001-#11999

Tool 1-999 height (H) offset

#12001-#12999

Tool 1-999 cutter compensation (D) wear

#13001-#13999

Tool 1-999 cutter compensation (D) offset

#30001-#30999

Tool 1-999 X wear

#31001-#31999

Tool 1-999 X offset

#32001-#32999

Tool 1-999 Y wear

#33001-#33999

Tool 1-999 Y offset

#34001-#34999

Tool 1-999 pocket

#2001-#2200

Tool 1-200 height wear (same values as #10001-#10200)

#2201-#2400

Tool 1-200 height offset (same values as #11001-#11200)

Lathe Tool Offsets

Pound Variable

Description

#10001-#10999

Tool 1-999 X wear

#11001-#11999

Tool 1-999 Z wear

#12001-#12999

Tool 1-999 nose radius wear

#13001-#13999

Tool 1-999 tool tip direction

#14001-#14999

Tool 1-999 Y wear

#15001-#15999

Tool 1-999 X offset

#16001-#16999

Tool 1-999 Z offset

#17001-#17999

Tool 1-999 nose radius offset

#19001-#19999

Tool 1-999 Y offset

#36001-#36999

Tool 1-999 tool slide

#1229 stores the current tool active in mach.

3.8 Naming #500 Series Variables

SETVN 500 [RADIUS]
SETVN 501 [X+ O&D]
SETVN 502 [X- O&D]
SETVN 503 [Y+ O&D]
SETVN 504 [Y- O&D]
SETVN 505 [LENGTH]

Example:

SETVN 500[X_TOP_LEFT, X_TOP_RIGHT, X_BOTTOM_LEFT, X_BOTTOM_RIGHT, Y_TOP_LEFT, Y_TOP_RIGHT, Y_BOTTOM_LEFT, Y_BOTTOM_RIGHT, FAST, SLOW]

#X_TOP_LEFT = -2
#X_TOP_RIGHT = 2
#X_BOTTOM_LEFT = -2
#X_BOTTOM_RIGHT = 2
#Y_TOP_LEFT = 2
#Y_TOP_RIGHT = 2
#Y_BOTTOM_LEFT = -2
#Y_BOTTOM_RIGHT = -2
#FAST = 1000
#SLOW = 500

G90 G01
X [#X_TOP_LEFT] Y [#Y_TOP_LEFT] F [#FAST]
X [#X_TOP_RIGHT] Y [#Y_TOP_RIGHT] F [#FAST]
IF [#501 EQ 1] THEN #3000 = 5
X [#X_BOTTOM_RIGHT] Y [#Y_BOTTOM_RIGHT] F [#FAST]
X [#X_BOTTOM_LEFT] Y [#X_BOTTOM_LEFT] F [#SLOW]
X [#X_TOP_LEFT] Y [#Y_TOP_LEFT] F [#SLOW]

M30
%

For more information about Macro B please see the Macro B Quick Reference Guide.


Keywords: Advanced M code  MCode M-Code G code GCode G-Code Macro B MacroB macros # pound variables