Macro Programming in CNC: Variables, Loops, and Conditional Logic
Macro programming in CNC enables dynamic, intelligent G-code by using variables, conditional statements, and loops. It transforms your CNC from a simple executor into a smart automation system capable of decision-making and parametric control.
🧮 What Is CNC Macro Programming?
Macro programming allows you to:
- Store values in variables (e.g.,
#100 = 25) - Use logic (e.g.,
IF [#100 GT 50] THEN...) - Repeat actions using loops (
WHILE...DO...END) - Create custom cycles, tool counters, probe routines, and more
It’s supported in most FANUC-style controls and similar G-code dialects.
🔢 Types of Variables
| Type | Range | Use Case |
|---|---|---|
| Local variables | #1–#33 | Temporary use inside subprograms |
| Common variables | #100–#199 | Global values like offsets or flags |
| System variables | #500+ | Machine-specific (tool lengths, etc.) |
Example:
#101 = 50 ; Store value in variable
G1 X#101 F100 ; Move X to 50
🧠 Conditional Logic (IF)
Use IF statements to trigger actions based on conditions.
#100 = 20
IF [#100 EQ 20] THEN #110 = 1
| Operator | Meaning |
|---|---|
| EQ | Equals |
| NE | Not equal |
| GT | Greater than |
| LT | Less than |
| GE | Greater or equal |
| LE | Less or equal |
🔁 Looping with WHILE–DO–END
Example: Drilling 5 holes in a row
#101 = 0
WHILE [#101 LT 5] DO1
G0 X[#101 * 20] Y0
G81 Z-10 R2 F150
#101 = #101 + 1
END1
✅ Repeats the drilling pattern 5 times, shifting X by 20 mm each loop.
🔄 FOR-Like Structure with Variable Increment
No direct FOR loop in G-code, but use WHILE like this:
#1 = 10
WHILE [#1 LE 50] DO1
G1 X#1
#1 = #1 + 10
END1
➡️ X will move to 10, 20, 30, 40, 50
⚙️ Using Macros in Subprograms
Combine macros with subprograms for powerful reusability:
#100 = 0
WHILE [#100 LT 3] DO1
M98 P1000
#100 = #100 + 1
END1
Subprogram O1000 can contain machining instructions that execute 3 times.
🧪 Real-World Use: Custom Bolt Hole Pattern
#100 = 8 ; Hole count
#101 = 50 ; Circle diameter
#102 = 0 ; Loop index
WHILE [#102 LT #100] DO1
#103 = [360 / #100] * #102
#104 = [#101 / 2] * COS[#103]
#105 = [#101 / 2] * SIN[#103]
G0 X#104 Y#105
G81 Z-10 R2 F150
#102 = #102 + 1
END1
✅ Dynamically calculates and drills a perfect bolt circle.
🛠️ Macro Examples & Tricks
| Application | Technique Used |
|---|---|
| Tool wear compensation | #500–range system variables |
| Probing cycles | Conditional + arithmetic logic |
| Part counters | #100 = #100 + 1 logic |
| Multi-depth machining | Looping with #depth decrement |
🧠 Tips for Safe Macro Use
- Use comments to explain every macro action
- Simulate in a safe environment before production
- Avoid overwriting reserved system variables
- Use unique variable ranges to avoid collisions
📘 Summary
Macro programming elevates G-code into a powerful language for automation. By using variables, conditions, and loops, you can:
- Reduce code duplication
- Adapt programs to different parts without rewriting
- Automate fixture and probing operations
- Create intelligent machining strategies
It’s the ultimate tool for advanced CNC programmers.
Leave a comment