---
title: Tutorial 26 Conditional G-code (Firmware Macros)
description: Logic for your machine.
---
Tutorial 26 Conditional G-code (Firmware Macros)
1. Lesson Header
- Lesson Number: 26
- Level: Advanced
- Title: Conditional G-code
- Estimated Duration: 35 Minutes
- Prerequisites: Lesson 25 (Machine State)
- What You Will Build: A "Smart Start" Macro.
2. Concept Introduction
The "Dumb" Machine.
Standard G-code is linear. It executes line 1, then line 2, forever.
Conditionals allow the machine to make decisions.
"If the bed is cold, heat it up."
"If the filament is out, pause."
"While Z < 100, keep printing."
This turns G-code from a "Playlist" into a "Program".
3. Machine State Explanation
Variables.
Different firmwares handle variables differently.
- RepRap Firmware (Duet): var myTemp = 200
- Klipper: {% set my_temp = 200 %} (Jinja2 syntax)
- Marlin: Limited support (often uses #define at compile time or specific M-codes).
We will focus on the Logic (If/Else/While), using a pseudo-code style that maps to modern firmwares.
4. Command Breakdown
- IF / ELSE: Conditional execution.
- WHILE: Loops based on a condition.
- M117: Display Message (useful for debugging logic).
5. Minimal Working Example
The "Smart Heat" Script (RepRap Style).
Check if the bed is already hot. If so, skip heating.
; Check if bed temp (sensors.analog[0].lastReading) is < 60
if sensors.analog[0].lastReading < 60
M140 S60 ; Set Bed Temp
M190 S60 ; Wait for Bed Temp
else
M117 Bed is already hot!
Klipper Style:
{% if printer.heater_bed.temperature < 60 %}
M140 S60
M190 S60
{% else %}
M117 Bed is already hot!
{% endif %}
6. Visual Representation
Interactive preview is available in the interactive reader.
7. Build Exercise
Task: Write a "Filament Load" Macro.
Logic:
1. Check if Hotend Temp > 180.
2. If NO -> Heat to 200 and Wait.
3. If YES -> Proceed.
4. Extrude 50mm of filament.
5. Retract 2mm (to prevent oozing).
6. Turn off Heater? (Maybe ask user).
8. Deep Insight Section
Safety Checks.
Macros are powerful for safety.
You can write a "Pre-Print Check":
- Is the door closed?
- Is the filament sensor triggered?
- Is the Z-probe deployed?
If any fail, M112 (Emergency Stop) or M0 (Pause).
9. Common Failure Modes
- Infinite Loops:
while true { M117 Hello }will freeze your printer. - Syntax Errors: Firmware parsers are strict. A missing space or bracket will cause the whole macro to fail silently or halt.
10. Real-World Application
Automatic Bed Leveling (G29).
G29 is a built-in macro.
It probes points, calculates a mesh, and enables compensation.
You can write your own:
"Probe 3 points. If the variance is > 0.1mm, abort print and beep."
11. Final Clean Version
The "Smart Purge" Macro (Klipper Syntax).
This script purges a line only if the extruder is hot enough.
[gcode_macro SMART_PURGE]
gcode:
{% if printer.extruder.temperature >= 190 %}
G90
G1 X0 Y0 Z0.4 F3000 ; Move to corner
G1 X100 E20 F1000 ; Purge line
G1 Z2.0 F3000 ; Hop up
{% else %}
M117 Extruder too cold to purge!
{% endif %}
12. Stretch Challenge
Challenge: Write a "Layer Timelapse" trigger.
Every layer change (detected by Z height change), move the head to X0 Y200 (Park), trigger a camera (M240), and resume printing.
Hint: You need to override the G1 command or use a "Post-Process" script in your slicer to insert this macro at every layer change.