Advanced G-Code Programming: Macros, Subprograms & Conditional Logic
Advanced CNC programming goes beyond simple motion commands. It incorporates macros, subprograms, and conditional logic, allowing for intelligent automation, reusable cycles, and powerful parametric programming.
This guide explores the deepest layers of G-code programming using real-world examples, across systems like Fanuc, Haas, Siemens, and others.
📘 Table of Contents
- Introduction to Macro Programming
- Subprograms (M98/M99)
- Macros with G65/G66/G67
- System Variables (#100+)
- Conditional Logic (IF, GOTO, WHILE, DO/END)
- Realtime Decisions in CNC
- Practical Applications
- Safety & Debugging Techniques
- Brand-Specific Notes
- Final Tips
1. 🧠 What is Macro Programming?
Macro programming allows you to define parameterized blocks of code that can be reused, customized, and evaluated at runtime. Think of it like CNC’s version of “functions” in programming languages.
Benefits:
- Reduces code duplication
- Enables smart behavior (like decision trees)
- Can react to sensor inputs or process data
- Highly customizable machining cycles
2. 🔁 Subprograms – M98 / M99
A subprogram is a separate block of G-code saved under a different program number that can be called multiple times.
🔹 Format
O1000 (Main Program)
M98 P2000 L3 ; Call subprogram O2000 three times
M30
O2000 (Subprogram)
G1 Z-1.0 F100.
G1 Z1.0
M99
M98calls the subprogram.M99returns to the main program.Lis repeat count.
🔹 Use Cases
- Drilling multiple holes
- Repeating motion at multiple coordinates
- Tool checks or safe positioning
3. ⚙️ G65/G66 – Calling Macros
🔸 G65 – Non-modal macro call
G65 P1000 A2.5 B4.0
P1000= O1000 programAandB= arguments passed as#1,#2, etc.
🔸 G66 – Modal macro call
G66 P1001 A2.5 B4.0
G1 X50 Y50
G1 X100 Y0
G67 ; Cancel modal macro
- Runs the macro every time a motion block is executed.
- Useful for probing or on-the-fly measurements.
4. 🔢 Variables in G-Code
CNC machines support variable usage via system variable ranges.
| Range | Description |
|---|---|
#1–#33 | Local variables (macro arguments) |
#100–#199 | User-defined general vars |
#500–#999 | Persistent across reboots |
#1000+ | System status values |
🔹 Example: Using Variables
#100 = 0
WHILE [#100 LT 5] DO1
G1 X[#100 * 10] F100.
#100 = #100 + 1
END1
5. 🧮 Conditional Logic – IF, WHILE, GOTO
You can make decisions based on variable values:
🔸 IF Statement
IF [#100 EQ 0] THEN
(MSG, Tool Not Detected)
M30
END
🔸 GOTO + Labels
IF [#101 GT 5] GOTO 50
...
N50 G1 Z50
🔸 WHILE Loop
#1 = 1
WHILE [#1 LE 10] DO1
G0 X[#1*10]
#1 = #1 + 1
END1
6. 🧭 Real-Time CNC Decision Making
Macros enable decision-making based on:
- Probe data
- Tool offsets
- Alarm states
- Timer values
- Sensor or I/O signals
🔹 Probe Example (Fanuc)
#505 = 0 ; Clear variable
G65 P9810 Z-10.0 F100. ; Run probe macro
IF [#505 GT 0.5] THEN #3000 = 1 (Part too high)
7. 🛠️ Real Applications
| Application | Tool Used | Description |
|---|---|---|
| Tool breakage detect | IF / G65 / Macro | Compare load + tool time |
| Multi-part machining | Subprograms | Process same part N times |
| Adaptive roughing | Variables + IF | Change feedrate based on depth |
| Pallet shuttles | Macro + M-code | Conditional start sequence |
| Smart probing | G66 + G65 | Auto-offset measurement |
8. 🧪 Safety, Debugging & Simulation
- Use dry-run (M01, block skip) on macro-heavy programs
- Monitor variables on the control screen
- Use
M0orMSGto pause or display debug info - In Fanuc, use
#3000=1to trigger custom alarms
9. 🏭 Brand-Specific Macro Notes
🟨 Fanuc
- Supports full macro logic
- Use
G65,G66,IF,WHILE - Persistent variables:
#500–#999
🟩 Haas
- Supports
G65,IF,WHILE - Macros optional (needs macro option enabled)
- Persistent memory:
#500–#699
🟦 Siemens
- Logic via
R,IF,TRANS,FRAME - Cycles created using Cycle 800+ or user cycles
- Supports inline math
🟥 Heidenhain
- Uses
Qparameters and logical IF - Structured conversation format
- Requires level 3 access for full logic
10. 💡 Final Tips
✅ Use macros for repeatability
✅ Use subprograms to modularize code
✅ Use conditional logic to avoid collisions and tool errors
✅ Never assume a variable value — always initialize it
✅ Comment generously for maintenance
📌 Summary
Advanced G-code programming with macros, subprograms, and conditional logic gives you the power to:
- Optimize cycle time
- Automate safety procedures
- Build reusable machining blocks
- Enable real-time intelligence in machining
🚀 Master these tools to elevate your CNC skills to engineering level.
Leave a comment