Second-Referencing Variables in Custom Macro
This technique allows operators to query a large number of values using only a few CNC commands.
Share




Here is a little-known technique that can help you query a large number of values with just a few custom macro commands. It can be used with any series of consecutively numbered variables, such as offset-related system variables, axis-related system variables, common variables and permanent common variables. Examples of when this technique can be helpful include:
• After using a spindle probe to determine how much raw material is on a surface to be machined. Maybe the probe has touched along a series of points on a rough casting and you wish to determine the high point. With this information, you can determine how many roughing passes to take.
• When querying a group of tool offset registers to determine whether any are inappropriately set. Perhaps the operator forgot to enter a value or the value is outside an acceptable range.
The trick lies in understanding how custom macro variables can be second-referenced. Consider these commands:
#100 = 3.25 (Set common variable #100 to a value of 3.25)
#33 = 100 (Set local variable #33 to a value of 100)
#32 = #[#33] (Set local variable #32 the value of #100, which is 3.25)
In the third command, the first hashtag character (#) of #[#33] tells the CNC that a variable will be referenced; the #33 inside the brackets tells the CNC which variable will be referenced. Variable #33 is currently set to a value of 100, so the CNC currently evaluates #[#33] as #100.
In custom macro terms, this is called second-referencing a variable, and by itself, it may not seem to have helpful implications. Consider, however, that the value of the second-referencing variable (#33 in our example) can be changed, meaning a different variable will be in play. If a loop is used that steps the second-referencing variable number, a large number of variables can be queried with but a few custom macro commands.
Consider this more realistic example: You have a large casting to machine, but you are unsure of how much raw material must be removed from a surface, and that amount of material may change with each new casting.
Say the material to be removed is on the right side (X plus) of the casting, and this side is 20 inches long. If you use a spindle probe to find the most positive position in the X axis on the right side of the casting, the high point, you will be able to calculate the worst-case condition in terms of the amount of raw material to be removed. With this information, you will be able to determine how many roughing passes to take. You then have the spindle probe perform a series of touches, say 40 of them at 0.5-inch increments. You make the probe store the results of these touches (40 X positions) in common variables #101 through #140. Now you need to determine the high point (largest value). Here is a series of commands that will do so:
(Determine largest X value of #101-#140)
#32 = #101 (Starting largest X position)
#33 = 102 (Current common variable number to test)
N10 WHILE [#33 LE 140] DO 1 (Start of loop, testing through #140)
IF [#[#33] GT #32] THEN #32 = #[#33] (Retain larger value, #[#33] or keep #32)
#33 = #33 + 1 (Step second-referencing value)
END 1 (End of loop; #32 is the largest X position in the group)
These six custom macro commands test 39 common variables. When the loop is finished, local variable #32 will contain the largest X value in the group of probed positions. This value then can be compared to the X position of the finished X surface to determine how much raw material must be removed and how many passes to take.
Second-referencing variables can also be used with tool offsets. System variables from #2001 on, for example, are commonly used with machining center CNCs to provide access to tool-length geometry offsets: #2001 for offset one, #2002 for offset two and so on.
Consider this series of commands that tests offset registers from 1 through 20. If any are set to zero, an alarm is sounded (possibly indicating that the operator forgot to set one of them).
(Test offsets 1-20)
#100 = 2001 (Current offset number to test)
N10 WHILE [#100 LE 2020] DO 1 (Start of loop, testing through offset 20)
IF [#[#100] NE 0] GOTO 11
#3000 = 100 (Offset set to zero)
N11#100 = #100 + 1 (Step second-referencing value)
END 1 (End of loop; if loop ends, all offset registers have a value)
These examples show how second-referencing variables can come in handy whenever you have a series of consecutively numbered custom macro variables to query.
Related Content
4 Tips for Staying Profitable in the Face of Change
After more than 40 years in business, this shop has learned how to adapt to stay profitable.
Read MoreCustom Workholding Principles to Live By
Workholding solutions can take on infinite forms and all would be correct to some degree. Follow these tips to help optimize custom workholding solutions.
Read More4 Rules for Running a Successful Machine Shop
Take time to optimize your shop’s structure to effectively meet demand while causing the least amount of stress in the shop.
Read MoreHow to Meet Aerospace’s Material Challenges and More at IMTS
Succeeding in aerospace manufacturing requires high-performing processes paired with high-performance machine tools. IMTS can help you find both.
Read MoreRead Next
Last Chance! 2025 Top Shops Benchmarking Survey Still Open Through April 30
Don’t miss out! 91ÊÓÆµÍøÕ¾ÎÛ's Top Shops Benchmarking Survey is still open — but not for long. This is your last chance to a receive free, customized benchmarking report that includes actionable feedback across several shopfloor and business metrics.
Read MoreAMRs Are Moving Into Manufacturing: 4 Considerations for Implementation
AMRs can provide a flexible, easy-to-use automation platform so long as manufacturers choose a suitable task and prepare their facilities.
Read MoreMachine Shop MBA
Making Chips and 91ÊÓÆµÍøÕ¾ÎÛ are teaming up for a new podcast series called Machine Shop MBA—designed to help manufacturers measure their success against the industry’s best. Through the lens of the Top Shops benchmarking program, the series explores the KPIs that set high-performing shops apart, from machine utilization and first-pass yield to employee engagement and revenue per employee.
Read More