Mastering G-Code Macros: Variables, Conditions, and Looping in CNC Programming
Meta Description: Learn how to use G-code macros with variables, IF conditions, and loops for advanced CNC automation. Includes real-world examples, tables, and explanations for Fanuc-style macro programming (Macro B).
🔍 What Are G-Code Macros?
Macros in CNC programming are similar to functions in software. They allow you to use variables, perform logical operations, loops, and write flexible and intelligent machining code.
- 🔢 Use
#to define or access a variable (e.g.,#100 = 25.4) - 🧠 Include logic (e.g.,
IF[#100 EQ 10] THEN...) - 🔁 Loop using
WHILEconstructs
📘 Variable Types in Macro B (Fanuc-style)
| Variable Range | Use | Description |
|---|---|---|
| #1 – #33 | Local | Reset after each subprogram call |
| #100 – #199 | Common | User-defined variables |
| #500 – #999 | System | Persistent variables, saved between sessions |
📗 Example 1: Basic Macro with Variables
#100 = 10 (X Start) #101 = 5 (Step Size) #102 = 5 (Repetitions) G0 Z5.0 WHILE[#102 GT 0] DO1 G0 X#100 G1 Z-2.0 F150. G0 Z5.0 #100 = [#100 + #101] #102 = [#102 - 1] END1
This macro moves in X-direction by 5 units, drills a hole, and repeats 5 times.
📗 Example 2: Conditional Movement
#200 = 1 IF [#200 EQ 1] THEN G0 X50 Y50 ELSE G0 X100 Y100 END
Simple conditional statement: if variable #200 equals 1, move to (50,50), else move to (100,100).
📊 Arithmetic and Functions
- Arithmetic: +, -, *, /, MOD
- Math Functions: SIN[], COS[], TAN[], SQRT[], ROUND[]
#101 = SQRT[49] #102 = [SIN[#101] * 100]
This calculates square root of 49, then applies a sine function to that result and scales by 100.
🔄 Looping Structures
#1 = 0 WHILE[#1 LT 3] DO1 G0 X[#1*10] G1 Z-3.0 F200. G0 Z5.0 #1 = [#1 + 1] END1
Loops through X = 0, 10, 20 and drills at each location.
📘 Example 3: Parametric Bolt Hole Pattern
#110 = 6 (Number of holes) #111 = 50.0 (Radius) #112 = 0 (Start angle) #113 = [360/#110] (Angle step) #120 = 0 (Counter) WHILE[#120 LT #110] DO1 #130 = [COS[#112 * 0.01745] * #111] #131 = [SIN[#112 * 0.01745] * #111] G0 X#130 Y#131 G81 Z-10 R1.0 F100. #112 = [#112 + #113] #120 = [#120 + 1] END1
This creates a 6-hole bolt circle using trigonometry inside a macro loop.
⚙️ Advanced Macro Tips
- Use macros to control tool life and wear
- Automate probing routines and measurement cycles
- Integrate sensors and conditional logic (e.g., check if spindle is running)
📈 Macro Programming Use Cases
| Use Case | Macro Benefit |
|---|---|
| Toolpath repetition | Reduce code, easier updates |
| Hole patterns | Custom bolt circles, arrays |
| Conditional movement | Smart part detection, probing |
| Automation | Adaptive processes, decision making |
🧩 Summary
Mastering macros transforms how you program CNC machines. With just a few lines of parametric logic, you can automate tasks, reduce code size, and build smarter machining routines that are adaptive and future-proof.
Macros are especially powerful in high-mix low-volume production and smart factory environments, offering flexibility, efficiency, and a professional touch to any CNC operation.
Leave a comment