Advanced G-Code Programming: Macros, Subprograms & Conditional Logic
As CNC machining evolves, so does the complexity of its programming. Beyond basic movement commands, advanced CNC control systems support parametric programming, subroutines, and conditional logic — giving programmers the power of automation, intelligence, and adaptability.
In this guide, you’ll learn how to use macros, loops, IF/WHILE conditions, subprograms, and more to build smart, reusable, and dynamic CNC code.
🧠 What Is Macro Programming?
Macro programming enables parameterized, reusable CNC code, with variables, logic statements, and modular subroutines.
Benefits:
- Reduce code duplication
- Automate repetitive cycles
- React to real-time machine conditions
- Reuse code across parts, fixtures, and tools
🔢 Parameter Types in Fanuc/Haas
| Variable Range | Type | Example |
|---|---|---|
| #1–#33 | Local variables | Temporary in subs |
| #100–#199 | Common (user) variables | Part count, logic |
| #500–#999 | System variables (machine) | Offsets, timers |
Example:
#100 = 25 (Set feedrate)
G01 X50 F#100
🌀 Subprograms: Modular CNC Logic
Subprograms let you write modular code blocks that can be called multiple times, saving space and enabling flexibility.
Format:
O1000 (Main Program)
...
M98 P2000 L3
...
O2000 (Subprogram)
G01 X100 Y0
M99
M-Code Reference:
| Code | Description |
|---|---|
| M98 | Call subprogram |
| M99 | Return from subprogram |
💡
Ldefines how many times to repeat the subroutine.
⚙️ G65 & G66 – Custom Macro Calls
G65 is used to call a macro with arguments:
G65 P9010 A10.0 B5.0
Example Macro:
O9010 (Simple Drilling Cycle)
#1 = #1 + 5
G81 X#1 Y#2 Z-10 R2 F100
M99
Amaps to#1,Bto#2, etc.- G66 sets modal macros (persistent until canceled with G67)
🧮 Conditional Logic: IF / GOTO / WHILE
IF Statement:
IF [#100 GT 10] THEN #3000 = 1 (TOOL COUNT EXCEEDED)
WHILE Loop:
#101 = 0
WHILE [#101 LT 5] DO1
G01 X[#101*10]
#101 = #101 + 1
END1
GOTO & Labels:
IF [#100 EQ 0] GOTO 500
...
N500 (Start Drill)
🔍 Real-World Macro Examples
✅ Part Counter
#100 = #100 + 1
IF [#100 GE 50] THEN #3000 = 2 (50 PARTS COMPLETED)
✅ Dynamic Peck Drill
#1 = 0
WHILE [#1 LT 10] DO1
G83 Z[#1*-2] Q0.5 R2
#1 = #1 + 1
END1
✅ Tool Life Monitor
#200 = #200 + 1
IF [#200 GT #300] THEN #3000 = 3 (TOOL CHANGE REQUIRED)
📊 Macro vs Subprogram: Key Differences
| Feature | Subprogram (M98) | Macro (G65/G66) |
|---|---|---|
| Repetition | Yes | Yes |
| Pass parameters | No (fixed code) | Yes (A, B, C → #1, #2…) |
| Control logic | No | Yes (IF, WHILE, GOTO) |
| Scope | Local Oxxxx | Global O9xxx |
🛡️ Error Handling with System Alarms
Use system alarms to stop the program when conditions fail.
| Code | Description |
|---|---|
| #3000 = n | Custom alarm, stops execution |
| #3006 = n | Message only, no stop |
Example:
IF [#5021 GT 100.0] THEN #3000 = 5 (Z AXIS OVER TRAVEL)
🧠 Tips for Writing Reliable Macro Code
- Always reset local variables at start
- Use
#3000alarms to catch bad logic - Comment generously for future debugging
- Test with simulation or block skip switches
- Use Fanuc’s diagnostic variables (#500–#999) to read position, timers, etc.
📚 CNC Controllers that Support Macros
| Brand | Macro Support | Notes |
|---|---|---|
| Fanuc | Yes (Macro B) | Must be enabled (parameter) |
| Haas | Yes | Full macro + system vars |
| Siemens | Yes (Advanced cycles) | Structured language |
| Heidenhain | Partial (Cycles only) | Limited GOTO/IF |
| Mazak | Limited | Mainly handled in CAM |
🔮 Future of G-Code Macros (2025–2030)
- AI-optimized macros from tool data
- CAM-generated conditional logic (auto probe adapt)
- Real-time macro rewriting from sensor input
- PLC-G-code hybrid for machine customization
- Macro libraries shared across global shops
✅ Final Thoughts
Macro and parametric programming turns G-code from a static list of moves into a dynamic, responsive, and intelligent language. Mastering macros allows CNC professionals to build more reliable, more automated, and more scalable processes.
💡 Think of macros as CNC automation — powered by code, not hardware.
▶️ Next Suggested Topic:
“CNC Probing & Inspection Cycles: Automating Measurement with G-Code”
Shall we continue with that subject?
Leave a comment