3dSynth

Tutorials / Custom G-code

Open in the interactive tutorials reader

---
title: Tutorial 17 Loops Conceptually
description: Understanding repetition, layers, and the "Unrolled Loop".
---

Tutorial 17 Loops Conceptually

1. Lesson Header

2. Concept Introduction

The "Unrolled" Loop.

In programming languages like Python or C++, you write:
for i in range(10): print(i)

In standard G-code, we don't usually have a for loop (unless using advanced macros). Instead, we must unroll the loop.
To print 10 layers, we must write the code for one layer, and then repeat that block of code 10 times in the file.

However, we don't want to rewrite the coordinates every time. We want to write the shape once (using Relative G91 or just repeating the X/Y moves) and simply change the Z height.

A Loop in G-code consists of three parts:
1. Initialization: Getting to the start (Homing, heating, moving to Layer 0).
2. The Body: The shape you want to print (The "Stamp" from Lesson 16).
3. The Increment: Moving the nozzle up by one layer height (G1 Z0.2).

3. Machine State Explanation

The Z-Iterator.

The Z-axis acts as our "loop counter".
Every time we complete a layer, we increment Z.
- State A: Z=0.2 (Layer 1)
- State B: Z=0.4 (Layer 2)
- State C: Z=0.6 (Layer 3)

The machine doesn't "know" it's looping. It just executes a long list of commands. But you, the programmer, know that lines 100-120 are the same as lines 121-140, just higher up.

4. Command Breakdown

G91 for Z-moves.

The most robust way to write a manual loop is to use Relative Positioning for the Z-change.
If you use G1 Z0.2 (Absolute), you go to height 0.2mm.
If you use G91 then G1 Z0.2, you go up by 0.2mm from wherever you are.

This allows you to copy-paste the "Layer Change" command without editing the number.

5. Minimal Working Example

The "Infinite" Square (Unrolled 3 times)

; --- SETUP ---
G28
G90
G1 Z0.2 F1000 ; Start at Layer 1

; --- LOOP ITERATION 1 ---
; Draw Square
G1 X10 Y0 E1
G1 X10 Y10 E1
G1 X0 Y10 E1
G1 X0 Y0 E1
; Increment Z
G91         ; Relative
G1 Z0.2     ; Up 0.2mm
G90         ; Absolute

; --- LOOP ITERATION 2 (Copy-Paste) ---
; Draw Square
G1 X10 Y0 E1
G1 X10 Y10 E1
G1 X0 Y10 E1
G1 X0 Y0 E1
; Increment Z
G91
G1 Z0.2
G90

; --- LOOP ITERATION 3 (Copy-Paste) ---
; Draw Square
G1 X10 Y0 E1
G1 X10 Y10 E1
G1 X0 Y10 E1
G1 X0 Y0 E1
; Increment Z
G91
G1 Z0.2
G90

6. Visual Representation

Think of a Slinky.

Interactive preview is available in the interactive reader.

From the top, it looks like a circle.
From the side, it is a continuous spiral.
Each "ring" of the slinky is one iteration of the loop. The "wire" is the filament path.

7. Build Exercise

Task: Create a 5-layer tall "Triangle" prism.
- Base: 20mm equilateral triangle.
- Layer Height: 0.3mm.
- Start at Z=0.3.

Steps:
1. Define the Triangle path (A->B->C->A).
2. Write the "Layer Change" block (Up 0.3mm).
3. Copy-paste the [Triangle + Layer Change] block 5 times.

The Math:
- Start at X10 Y10.
- Side length 20.
- Vertex 2: X30 Y10.
- Vertex 3: X20 Y27.32 (approx height of equilateral triangle is side * sqrt(3)/2).
- Return to X10 Y10.

8. Deep Insight Section

Slicers are Loop Generators.
When you slice a generic STL file, the software is doing exactly what we are doing:
1. It calculates the 2D polygon for the current Z height.
2. It writes the G-code for that polygon.
3. It adds a Z-move.
4. It repeats for the next layer.

The only difference is that Slicers change the 2D polygon slightly every layer (to make complex 3D shapes), whereas our manual loop repeats the exact same polygon (making a prism).

9. Common Failure Modes

  1. Accumulating E-values: If you use Absolute Extrusion (M82), you must reset the extruder (G92 E0) at the start of every loop iteration. Otherwise, your first line tries to extrude to E1, and your second loop tries to extrude to E1 again (which is 0 extrusion because it's already there!).
  2. Drifting Z: If your "Layer Change" code is complex, ensure it always ends in G90 so the X/Y moves of the next layer go to the correct absolute coordinates.

10. Real-World Application

Vase Mode (Spiralize Outer Contour).
"Vase Mode" is a special kind of loop where the Z-move is not a separate step at the end. Instead, Z increases gradually during the X/Y moves.
- Standard Loop: Move X/Y -> Move Z -> Move X/Y -> Move Z.
- Spiral Loop: Move X/Y/Z simultaneously.

We will cover Spiral Vase Mode manually in Lesson 18.

11. Final Clean Version

Save this as lesson17.gcode.

; Lesson 17 - The Manual Loop
G21
G90
M83 ; Relative Extrusion (Essential for loops!)
G28
G1 Z0.3 F1000 ; Initial Height

; --- DEFINING THE LOOP BODY ---
; We will repeat this block 5 times.
; Shape: A 20mm square at center (100,100)

; [ITERATION 1]
G0 X90 Y90 F3000 ; Move to start
G1 X110 E1 F1200 ; Side 1
G1 Y110 E1       ; Side 2
G1 X90 E1        ; Side 3
G1 Y90 E1        ; Side 4
; Z-Hop
G91
G1 Z0.3
G90

; [ITERATION 2]
G0 X90 Y90
G1 X110 E1
G1 Y110 E1
G1 X90 E1
G1 Y90 E1
G91
G1 Z0.3
G90

; [ITERATION 3]
G0 X90 Y90
G1 X110 E1
G1 Y110 E1
G1 X90 E1
G1 Y90 E1
G91
G1 Z0.3
G90

; [ITERATION 4]
G0 X90 Y90
G1 X110 E1
G1 Y110 E1
G1 X90 E1
G1 Y90 E1
G91
G1 Z0.3
G90

; [ITERATION 5]
G0 X90 Y90
G1 X110 E1
G1 Y110 E1
G1 X90 E1
G1 Y90 E1
G91
G1 Z0.3
G90

G28 X0 Y0 ; Park

12. Stretch Challenge

Challenge: Create a "Pyramid" loop.
5 Layers.
Layer 1: 50mm square.
Layer 2: 40mm square.
Layer 3: 30mm square.
Layer 4: 20mm square.
Layer 5: 10mm square.

Hint: You cannot just copy-paste the shape. You must change the coordinates for each iteration. This is a "Parametric Loop" where the parameter (Size) changes with the Loop Counter (Z).