Advanced G-Code Techniques: Subprograms, Macros, and Conditional Logic Explained
Basic G-code gets the job done. But if you’re looking to write smarter, faster, more flexible CNC programs, you need to go beyond simple G01s and G81s.
This guide introduces advanced G-code tools like:
- Subprograms (M98, M99)
- Custom Macros (G65, G66)
- Variables and Conditional Logic
Used correctly, these features can reduce code length by 70% and allow full parametric programming.
🔁 Subprograms: Reuse Code with M98/M99
Instead of copy-pasting blocks over and over, subprograms let you call a sequence as many times as needed.
🔍 Basic Structure:
M98 P1234 L5 (Call subprogram O1234 five times)
...
O1234
G01 X10 Y10
G01 X20 Y10
M99 (Return to main program)
✅ Benefits:
- Clean, reusable code
- Easier to debug
- Better for repeating patterns (e.g., bolt hole circles)
💡 Tip: Subprogram number =
Onumber. M98 calls, M99 returns.
🧮 Macros: Parametric, Smart G-Code with G65
G-code macros let you create functions with inputs. Like calling a CNC “subroutine” with variables.
🔍 Example:
G65 P9100 A10.0 B20.0 D5.0
- Calls program
O9100 - Passes values A=10.0, B=20.0, D=5.0
Inside O9100:
O9100
#1 = #1 + #2 (Math using passed variables)
G81 X#1 Y#2 R2 Z-#3 F100
M99
✅ Benefits:
- Reduce 500 lines of code into 20
- Easily update dimensions with new inputs
- Full parametric control
💡 Fanuc Macro B allows this structure. Not all machines support it by default.
🔀 Conditional Logic: Add CNC “If” Statements
Add decisions into your program — like skipping code, repeating logic, or comparing variables.
🔍 Example:
#100 = 5
IF [#100 GT 10] THEN #100 = 10
Or loop:
WHILE [#100 GT 0] DO1
(Do something)
#100 = #100 - 1
END1
✅ Use Cases:
- Probe-based decisions
- Repeat-until loops
- Conditional tool changes
- Error trapping
🧠 Variable Types
| Variable Type | Range | Use Case |
|---|---|---|
| Local (#1–#33) | Reset each call | Temporary values in macros |
| Common (#100–#199) | Persistent | Store setup data across programs |
| System (#500–#999) | Machine use | Tool lengths, probe offsets |
🧰 Macro Tips for Professionals
- Always comment what each variable means
- Use
#100+only when necessary — it overwrites memory - Test logic in simulation before going live
- Use macros with probing cycles for zeroing automation
- Store material-specific feedrates using common variables
🧠 Real-World Applications
✅ Bolt hole pattern generator
✅ Part family programs (same shape, different sizes)
✅ Probing with decision logic
✅ Auto offset setting and tool inspection
✅ “Smart” safety routines (collision prevention)
📘 Example: Bolt Hole Circle Macro
Main Program Call:
G65 P8800 X0 Y0 D100 Q6 (Center X/Y, 100mm diameter, 6 holes)
Macro:
O8800
#1 = #24 (X center)
#2 = #25 (Y center)
#3 = #7 (Diameter)
#4 = #17 (Hole count)
#5 = 0
WHILE [#5 LT #4] DO1
#6 = 360 / #4 * #5
#7 = COS[#6] * (#3 / 2)
#8 = SIN[#6] * (#3 / 2)
G81 X[#1 + #7] Y[#2 + #8] R2 Z-10 F100
#5 = #5 + 1
END1
M99
🎯 Final Thoughts
Advanced G-code programming isn’t just for aerospace shops — it’s for any CNC programmer who wants:
- Shorter, smarter code
- Easier part family management
- Built-in safety logic
- Higher automation with fewer errors
Learn it once, and it will pay off in every job you run.
Leave a comment