Subprograms and Macros: How to Automate Repetitive Tasks in G-Code.

The "Copy-Paste" Trap

Imagine you have a fixture holding four identical parts. You need to drill the same bolt circle on all four.

The rookie programmer writes the drilling cycle for Part 1, then copies and pastes that code three more times, changing the work offset (G54, G55, G56, G57) for each block.

Suddenly, the engineer walks in: "The hole depth needs to be 0.100 inches deeper."

Now you have to find that Z-depth value in four different places in your code and edit them one by one. If you miss one? You scrap a part.

There is a better way. By using Subprograms and Macros, you can write the cutting logic once and call it as many times as you need. It makes your code shorter, smarter, and virtually bulletproof against editing errors.

Here is how to automate your G-code.

1. Subprograms (M98 and M99): The "Repeat" Button

Think of a subprogram as a music playlist loop. You have a main song (Main Program), but for the chorus, you reference a separate track.

  • M98: Calls the subprogram (Jumps to it).

  • M99: Ends the subprogram (Returns to the main program).

The Use Case: You have a complex contour or a hole pattern that needs to be performed at multiple locations or on multiple parts.

The Workflow: Instead of rewriting the coordinates for the contour every time, you put the contour in a separate program (e.g., O2000).

Example Code:

Main Program (O1000):

G-Code

G00 G90 G54 X0 Y0 (Move to first part) M98 P2000 (Jump to Subprogram O2000 to cut the feature) G00 G55 X0 Y0 (Move to second part) M98 P2000 (Jump to Subprogram O2000 again) M30 (End Main Program)

Subprogram (O2000):

G-Code

G01 Z-0.5 F10. (Cut geometry) X1.0 Y1.0 X0 Y0 G00 Z1.0 (Retract) M99 (Return to Main Program)

Why this wins: If you need to change the feed rate or the depth, you change it once in program O2000, and it automatically updates for every part on the table.

2. Macros (G65): The "Smart" Code

If Subprograms are for repeating tasks, Macros are for calculating tasks.

Macro programming (often called "Macro B" on Fanuc controls) allows you to use Variables (like #500) and Logic (IF/THEN statements). This turns your CNC control into a calculator.

The Use Case: "Families of Parts" Imagine you make a washer. Sometimes the customer wants it 1" diameter, sometimes 2" diameter, sometimes 5". instead of writing three different programs, you write one Macro that accepts the diameter as a variable.

The Magic Command: G65 G65 works like M98 (it calls a subprogram), but it allows you to pass arguments (variables) into that subprogram.

Example Code:

Main Program:

G-Code

(Machining a 3.0 inch circle) G65 P8000 D3.0 F20.

Macro Program (O8000):

G-Code

(Variables passed: D becomes #7, F becomes #9) #1 = #7 / 2 (Calculate Radius: 3.0 / 2 = 1.5) G01 X#1 F#9 (Move to Radius X1.5 at Feed 20.) G03 I-#1 (Cut Full Circle) M99

Why this wins: You now have a "Universal Circle Generator." You never need to program a circle again. You just type G65 P8000 and tell it what size you want.

3. Local vs. Common Variables

To use Macros effectively, you must understand the two main types of variables:

  • Local Variables (#1 - #33): These are temporary. They only exist inside the macro you are currently running. When the macro finishes (M99), they are wiped clean. These are used for math inside the cycle.

  • Common Variables (#500 - #999): These are permanent. They stay in the machine memory even after you turn the power off.

    • Pro Tip: Use Common Variables for persistent data, like part counters or probe calibration data.

4. Logic: IF, THEN, and GOTO

This is where true automation happens. You can teach the machine to make decisions.

Example: Tool Life Management

G-Code

#501 = #501 + 1 (Counter: Add 1 to variable #501 every cycle) IF [#501 GT 100] GOTO 9000 (If part count is greater than 100, jump to line N9000) ... (Machining code) ... M30 N9000 #3000 = 1 (TOOL EXPIRED - CHANGE INSERT) (This generates an alarm message on the screen and stops the machine)

This simple logic prevents you from running a dull tool and scrapping parts. The machine tracks the count for you and stops itself when it's time for maintenance.

Summary: Work Smarter, Not Harder

Manual programming isn't about typing every single coordinate; it's about building tools that do the work for you.

  • Use Subprograms to handle repeating geometry (patterns).

  • Use Macros to handle changing geometry (families of parts).

Once you build a library of trusted subprograms, you will program faster and with fewer errors than you ever thought possible.

Ready to Build Your Macro Library?

Macro programming can feel like learning a new language, but you only need a few key templates to get started. Machining Tutor

JOIN OUR MAILING LIST

Machining Tutor is the premier online training platform for future CNC professionals.

We combine immersive, real-world video lessons with 24/7 AI Mentorship and Live 1-on-1 Classes to take you from 'Zero Knowledge' to 'Job-Ready' in record time.

Stop guessing and start mastering G-Code, CAD/CAM, and Machine Setup today.

G Code LTD

71-75 Shelton Street

London, United Kingdom

Newsletter

Subscribe now to get daily updates.