← Back to CNC Programming

Macro Programming Foundations

Macro programming adds variables, arithmetic, and logic to G-code. Instead of hard-coding every coordinate, the machine calculates it. This page introduces the core concepts — variables, IF/GOTO, WHILE loops, and a bolt-circle example — as the entry point to advanced CNC automation.

Concept

Basic G-code is static: every coordinate, feed, and depth is written as a fixed number. A bolt circle of 12 holes means 12 X/Y lines. A rectangular pocket means dozens of step-over lines. Macro programming adds the tools of a simple programming language — variables, math operations, conditional logic, and loops — directly into the CNC program.

Variables are written as #1, #2, and so on. You can assign them values, do arithmetic on them, and use them in coordinates. G00 X#1 moves to whatever value #1 currently holds. With a WHILE loop, the machine can drill 12 holes around a circle by computing each position from trigonometry instead of you typing every coordinate.

This page covers the fundamentals. Full macro programming (deep parameterization, probing, custom canned cycles) is an advanced topic built on these basics.

Why It Matters

Consider drilling 12 holes on a bolt circle of radius 50 mm. Without macros, you calculate each hole position by hand:

G81 X50.0 Y0.
X43.3 Y25.0
X25.0 Y43.3
X0.0 Y50.0
... (12 lines total)

With a macro, you write a loop that computes each angle from 0 to 360 in 30° steps:

#1 = 0
WHILE [#1 LT 360] DO 1
  #2 = 50 * COS[#1]
  #3 = 50 * SIN[#1]
  G81 X#2 Y#3 R2. Z-10. F100.
  #1 = #1 + 30
END 1

Now change the bolt circle radius from 50 to 60: you edit one number (#2 = 60 * COS[#1]). Change from 12 holes to 8: edit the step from 30 to 45. No recalculating coordinates. This is the power of parameterized programming.

How It Works

Variables

Variables are storage locations named #1 through #n. You assign values, do math, and use them in G-code:

#1 = 10.          (assign 10 to #1)
#2 = 20.
#3 = #1 + #2      (#3 = 30)
#4 = #1 * 2       (#4 = 20)
G00 X#1 Y#2       (rapid to X10 Y20)
G01 Z-#3 F100.    (feed to Z-30)

Variables can hold coordinates, depths, feed rates, angles, or any numeric value. The # prefix tells the controller "look up the value in this variable" instead of treating it as a literal number.

Arithmetic and Functions

OperationSyntaxExample
Add#1 + #2#3 = #1 + #2
Subtract#1 - #2#3 = #1 - #2
Multiply#1 * #2#3 = #1 * #2
Divide#1 / #2#3 = #1 / #2
SineSIN[angle]#3 = 50 * SIN[#1]
CosineCOS[angle]#2 = 50 * COS[#1]
Square rootSQRT[#1]#4 = SQRT[#1]

Conditional Logic: IF / GOTO

IF makes decisions. GOTO n jumps to line number Nn. This is the simplest form of branching:

#1 = 10.
IF [#1 GT 10] GOTO 100   (if #1 > 10, jump to N100)
(something when #1 <= 10)
N100 (continues here)

Comparison operators: EQ (equal), NE (not equal), GT (greater than), LT (less than), GE (greater/equal), LE (less/equal).

Loops: WHILE / DO / END

WHILE repeats a block of code while a condition is true:

#1 = 0
WHILE [#1 LT 360] DO 1
  (do something)
  #1 = #1 + 30
END 1

The loop starts with #1 = 0. Each pass, it runs the body, then adds 30 to #1. When #1 reaches 360, the condition #1 LT 360 is false and the loop exits. The DO 1 / END 1 pairs define which block belongs to which loop.

Center 0° 60° 180°
Six holes on a bolt circle, 60° apart. A WHILE loop computes each X/Y from COS and SIN of the current angle.

Example

Bolt circle macro: 6 holes on radius 50 mm, centered at G54 X0 Y0. Hole depth Z-10, R plane Z2, feed 100 mm/min. Start at angle 0°, advance 60° per hole.

O0036 (BOLT CIRCLE MACRO)
G21 G17 G90 G40 G80
G54
T01 M06                 (Ø6 DRILL)
S2000 M03
G00 X0 Y0
G43 H01 Z50. M08

#1 = 0                  (angle in degrees, starts at 0)
WHILE [#1 LT 360] DO 1
  #2 = 50 * COS[#1]     (X = radius * cos(angle))
  #3 = 50 * SIN[#1]     (Y = radius * sin(angle))
  G99 G81 X#2 Y#3 R2. Z-10. F100.
  #1 = #1 + 60          (next hole: +60 degrees)
END 1

G80
G00 Z50. M09
M30

Trace: Iteration 1: angle=0, X=50×COS(0)=50, Y=50×SIN(0)=0 → drill at (50,0). Iteration 2: angle=60, X=25, Y=43.3 → drill at (25,43.3). Continues for angles 120, 180, 240, 300. When angle reaches 360, loop exits. Six holes drilled, one loop. To change to 8 holes, change #1 = #1 + 60 to +45. To change radius, edit the 50.

Common Mistakes

Practice

1. What is #1 = 10. used for?

Ans

It assigns the value 10 to variable #1. Later, G00 X#1 moves to X10. The variable acts as a named storage location.

2. Why use a loop instead of writing 12 hole positions by hand?

Ans

Maintainability: change the radius or hole count in one place and the whole pattern updates. No manual trigonometry, no typos in coordinates.

3. You want 8 holes instead of 6 on the bolt circle. What do you change?

Ans

The angle increment from 60 to 45 (360/8 = 45 degrees). The loop automatically runs 8 times: 0, 45, 90, 135, 180, 225, 270, 315.

4. What happens if you forget #1 = #1 + 60?

Ans

The loop condition #1 LT 360 stays true forever (angle never changes). The machine drills the same hole repeatedly until you press feed hold.