CNC Macro Programming: Variables, Logic, and Automation
Macro programming brings the power of logic, decision-making, and automation to CNC machines. It allows for dynamic, intelligent code that responds to conditions, reducing manual intervention and optimizing efficiency.
Whether you’re building custom cycles, automated probing routines, or tool life management, macros turn a static CNC into a smart one.
🧩 What Is CNC Macro Programming?
CNC macro programming is a type of parametric programming that uses:
- Variables
- Mathematical expressions
- Logical operators
- Loops and conditionals
Instead of hard-coded numbers, you use flexible parameters like #100, enabling more versatile, reusable code.
🔢 Types of Variables
| Type | Syntax | Description |
|---|---|---|
| Local Variables | #1–#33 | Used within subprograms/macros |
| Common Variables | #100–#199 | Retain values across programs |
| System Variables | #3000+ | Control alarms, machine state, settings |
Example:
#100 = 25 ; Set value
G0 X#100 ; Move to X=25
🔧 Basic Operators
| Operator | Function |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
MOD | Modulo (remainder) |
EQ | Equals |
NE | Not equal |
GT | Greater than |
LT | Less than |
🧠 IF/THEN Logic
IF [#100 EQ 10] THEN #101 = 5
If #100 equals 10, then set #101 to 5.
🔁 WHILE Loops
#100 = 0
WHILE [#100 LT 5] DO1
G0 X[#100*20]
#100 = [#100 + 1]
END1
Creates a loop that runs 5 times, spacing each move by 20 mm.
⚠️ Error Handling with Alarms
IF [#100 GT 100] THEN #3000=1 (Value Too High!)
This halts the program and shows a custom alarm if the condition is met.
🧪 Real-World Example: Drilling with Variable Spacing
#101 = 0 ; Counter
#102 = 20 ; Hole spacing
WHILE [#101 LT 5] DO1
G81 X[#101 * #102] Y0 Z-10 R1 F100
#101 = [#101 + 1]
END1
G80
Drills 5 holes with 20 mm spacing using one loop block.
🧭 Dynamic Toolpath Control
You can adjust depths, feeds, or tool positions based on tool wear or probing results.
#2000 = #1000 + #500 ; Adjust Z based on probe offset
G1 Z[#2000] F100
🔄 Custom Canned Cycles with Macros
Build your own cycles like bolt circles or slot milling.
Example: Bolt circle pattern
#100=6 ; Number of holes
#101=50 ; Circle radius
#102=360/#100 ; Angle step
#103=0 ; Initial angle
WHILE [#103 LT 360] DO1
#110 = [COS[#103] * #101]
#111 = [SIN[#103] * #101]
G81 X#110 Y#111 Z-10 R1 F100
#103 = [#103 + #102]
END1
G80
🔐 Best Practices
- Always initialize variables before use
- Use comments to explain logic
- Test in simulation before live run
- Avoid infinite loops — ensure exit conditions
- Use
#3000for controlled stops if errors arise
🔍 Where Macros Excel
| Use Case | Benefit |
|---|---|
| Probing routines | Measure and update offsets |
| Repetitive hole patterns | One block replaces 50 lines |
| Dynamic feeds and depths | Adjust for material or tool wear |
| Alarm handling | Built-in error logic |
| Automated part programs | One code serves multiple variations |
✅ Summary
Macro programming is like writing software inside your G-code:
- Use variables for dynamic values
- Apply logic for smarter decisions
- Automate repetitive tasks
- Create custom, modular cycles
- Increase flexibility and reduce errors
By mastering macro programming, you’re not just coding — you’re engineering intelligent automation inside your CNC controller.
Leave a comment