Slick tricks for CNC’s: Simple parts counter using Macros


You’re running a small shop. You have a production bar feed job on a lathe that runs pretty steady, but still,  you want to keep an eye on it. You don’t want to dedicate yourself or an employee to keeping an eye on it full time, but you don’t want run the job fully unattended (you might forget that you need to check on it occasionally and wind up running half the job out of tolerance).

CNC’s, at least older ones give you two choices how to handle end of program.

  1. Put an M30 at the end: stop and rewind. You have to press the start button every time to make a piece.
  2. Put an M99 at the end: loop program. It will run forever.

Wouldn’t it be nice to tell the machine “I only want to check, say, every 5th piece so I only want to run 5 pieces and the the machine stops until I get back to it.”

Here’s a simple way to do it with any machine with macro capabilities.

This particular code is tailored for a Fanuc 21T:


At the beginning, before the program runs anything operation put this line in:

N1 IF[#505GE#506]GOTO999

At the end of the program, don’t put a M99 or M30 or any end of program command. Instead put this code in:

N2 #505=#505+1(PART COUNTER,)
N3 M#501 (<— Set Macro var 501 to 99. The control will see “M99”)

Before you run this, go to your macro variable page (in Fanucs its the offset key. hit the right soft key until you see macro and hit that soft key.)

  1. Set macro variable 506 to the amount of piece you want to run, in this example make 506 equal to 5
  2. Set 505 to zero. This will be the counter.
  3. Set 501 to 99. Then in line N3 above the control will apply 99 to the M word and it will loop at that point, ignoring anything below it. That includes ignoring line N999.

Line N1 is where the machine sees whether it made enough pieces It’s a standard IF/THEN statement. It states:

IF the value in #505 is less than the value in #506 then ignore and just continue, however:

IF the value in #505 is greater than or equal to the value in #506 then jump to line N999

Line N999 resets var to zero and the the next line executes the M30 program stop and rewind. When you are ready just hit the start button.

Simple elegant and powerful in giving your shop a little more automation.

Macros can control just about anything on your CNC; very powerful. With power comes risk. A few things to be aware of.

  • On Fanucs any var below 500 are usually volatile, meaning that if you shut off the machine or even hit reset they get wiped out.
  • 500 through 531 are generally safe to use for your own programs, providing the machine builder or some other feature doesn’t use them. These stay even when the machine is shut down and will not change unless you, your program ( or someone else program, such as a bar feeder routines).  If they’re all zero or blank (called null) when you start you should be safe to use them, but keep an eye on them before you start changing them. If you’re not sure, consult you machine builder dcocument as well as add on documents for thing like bar feeders and loaders.
  • It’s a REALLY good idea to keep macro variable list with descriptions of what you’re using them for, basically a map so you know down the road why, for example, 506 is set to 21. This particularly helps if you do this across multiple machine. I have maps for all my machine tool.

Download the file for the sample program. Good luck!

Comments are closed.