📖 Scenario: You are programming a CNC machine to cut a simple shape multiple times on a metal sheet. To avoid repeating the same code for each shape, you will create a subprogram that defines the shape once and call it multiple times with different positions.
🎯 Goal: Create a CNC program that uses a subprogram to cut a square shape repeatedly at different positions on the metal sheet.
📋 What You'll Learn
Create a main program with a subprogram called O1000 that cuts a square.
Call the subprogram O1000 three times with different X and Y coordinates.
Use GOTO or CALL commands to invoke the subprogram.
Include comments to explain each part of the program.
💡 Why This Matters
🌍 Real World
CNC programmers often use subprograms to repeat common shapes or operations efficiently without rewriting code multiple times.
💼 Career
Understanding subprograms is essential for CNC operators and programmers to write clean, maintainable, and efficient machining programs.
Progress0 / 4 steps
1
Create the main program and subprogram skeleton
Write the main CNC program starting with O0001 and create an empty subprogram called O1000. Include program start and end commands % and M30. Do not add any cutting commands yet.
CNC Programming
Hint
Start with program number O0001 and define subprogram O1000 with no commands inside yet.
2
Add the square cutting commands inside the subprogram
Inside the subprogram O1000, add commands to cut a 10x10 mm square starting from the current position. Use G01 linear moves to draw the square edges. End the subprogram with M99 to return to the main program.
CNC Programming
Hint
Use G01 commands to move in a square shape and M99 to end the subprogram.
3
Call the subprogram three times at different positions
In the main program O0001, call the subprogram O1000 three times. Before each call, move the tool to a new position using G00 rapid moves to coordinates X0 Y0, X20 Y0, and X40 Y0. Use M98 P1000 to call the subprogram.
CNC Programming
Hint
Use G00 to move quickly to each start point and M98 P1000 to call the subprogram.
4
Print the final CNC program output
Print the entire CNC program code including the main program and subprogram exactly as written, showing the repeated calls and the square cutting commands.
CNC Programming
Hint
Use print() statements to output each line of the CNC program exactly.
Practice
(1/5)
1. What is the main purpose of using a subprogram in CNC programming?
easy
A. To stop the machine immediately
B. To speed up the machine spindle
C. To change the tool automatically
D. To repeat a set of instructions multiple times without rewriting them
Solution
Step 1: Understand subprogram concept
Subprograms are blocks of code that can be reused multiple times in CNC programming.
Step 2: Identify main purpose
They help avoid rewriting the same instructions repeatedly, making code shorter and easier to manage.
Final Answer:
To repeat a set of instructions multiple times without rewriting them -> Option D
Quick Check:
Subprograms repeat code = D [OK]
Hint: Subprograms repeat code blocks to save writing [OK]
Common Mistakes:
Confusing subprograms with tool changes
Thinking subprograms control spindle speed
Assuming subprograms stop the machine
2. Which of the following is the correct syntax to call a subprogram numbered 100 in CNC code?
easy
A. M99 P100
B. M98 P100
C. M100 P98
D. M98 L100
Solution
Step 1: Recall subprogram call command
M98 is the standard command to call a subprogram in CNC programming.
Step 2: Match syntax with subprogram number
The correct syntax is M98 followed by P and the subprogram number, e.g., M98 P100.
Final Answer:
M98 P100 -> Option B
Quick Check:
Call subprogram = M98 P[number] [OK]
Hint: Use M98 P[number] to call subprograms [OK]
Common Mistakes:
Using M99 to call instead of return
Swapping M98 and M99 commands
Using L instead of P for subprogram number
3. Given the following CNC code snippet, what will be the output movement commands executed?
C. L0 causes no repetition, so subprogram never runs
D. Missing G00 command before M98
Solution
Step 1: Check repetition count in subprogram call
L0 means repeat zero times, so the subprogram is not executed at all.
Step 2: Confirm other commands are correct
M99 is correct to return from subprogram, P2000 is valid, and G00 is not mandatory before M98.
Final Answer:
L0 causes no repetition, so subprogram never runs -> Option C
Quick Check:
L0 means zero repeats = B [OK]
Hint: L0 means no repeats, so subprogram won't run [OK]
Common Mistakes:
Thinking M99 is wrong inside subprogram
Assuming P2000 is invalid
Believing G00 is required before M98
5. You want to drill 5 holes spaced 10mm apart along the X-axis starting at X0 Y0 using a subprogram. Which code snippet correctly uses a subprogram with a loop to achieve this?