KevsRobots Learning Platform
By Kevin McAleer, 5 Minutes
Bigger projects have parts that belong together: everything on the robot, everything on your laptop. A subgraph draws a box around a group of nodes.
flowchart LR
subgraph robot [On the robot]
pico[Pico W] --> motors[Motors]
sensor[Distance sensor] --> pico
end
subgraph laptop [On my laptop]
app[Control app]
end
app -- Wi-Fi --> pico
Here’s what it draws:
flowchart LR
subgraph robot [On the robot]
pico[Pico W] --> motors[Motors]
sensor[Distance sensor] --> pico
end
subgraph laptop [On my laptop]
app[Control app]
end
app -- Wi-Fi --> pico
A subgraph starts with subgraph, an ID and an optional title in square brackets, and finishes with end. You can link to a whole subgraph by its ID too, like app --> robot.
You can also set a direction inside a subgraph with a direction line:
flowchart TB
subgraph power [Power]
direction LR
bat[Battery] --> reg[Regulator] --> pico[Pico]
end
flowchart TB
subgraph power [Power]
direction LR
bat[Battery] --> reg[Regulator] --> pico[Pico]
end
A class is a named style you can reuse. Define it once with classDef, then apply it to as many nodes as you like:
flowchart LR
classDef hardware fill:#ffd166,stroke:#333,color:#000
classDef software fill:#06d6a0,stroke:#333,color:#000
sensor[Ultrasonic sensor]:::hardware --> code[distance.py]:::software
code --> motor[Motor driver]
class motor hardware
Here’s what it draws:
flowchart LR
classDef hardware fill:#ffd166,stroke:#333,color:#000
classDef software fill:#06d6a0,stroke:#333,color:#000
sensor[Ultrasonic sensor]:::hardware --> code[distance.py]:::software
code --> motor[Motor driver]
class motor hardware
There are two ways to apply a class:
:::className straight after a nodeclass line with one or more node IDs: class motor,sensor hardwareThe style properties are like CSS: fill is the background, stroke is the border, color is the text, and you can add stroke-width:2px or stroke-dasharray:5 5 for a dashed border.
For a single node, style is quicker than a class:
flowchart LR
ok[All good] --> warn[Battery low]
style warn fill:#ef476f,color:#fff
flowchart LR
ok[All good] --> warn[Battery low]
style warn fill:#ef476f,color:#fff
Links don’t have IDs, so linkStyle uses their position, counting from 0 in the order you wrote them:
flowchart LR
a[Sensor] --> b[Pico] --> c[Motor]
linkStyle 1 stroke:#ef476f,stroke-width:3px
flowchart LR
a[Sensor] --> b[Pico] --> c[Motor]
linkStyle 1 stroke:#ef476f,stroke-width:3px
Here link 0 is Sensor to Pico, and link 1 is Pico to Motor.
Go easy on colour. Two or three colours that mean something (hardware, software, warnings) help a reader; a rainbow just gets in the way.
These catch everyone at least once.
1. A node called end. end closes a subgraph, so this breaks:
flowchart LR
start --> end
Capitalise it or give it a different ID: start --> End or start --> finish([End]).
2. IDs starting with o or x. Mermaid reads --o and --x as special arrow heads, so pico---ops becomes a circle-ended line to a node called ps! Add a space, or capitalise: pico --- ops or pico---Ops.
3. Special characters in labels. Brackets, quotes and some symbols confuse Mermaid. Wrap the label in double quotes, and write a quote mark inside a label as #quot;:
flowchart LR
a["Read A0 (0-65535)"] --> b["Print #quot;hello#quot;"]
flowchart LR
a["Read A0 (0-65535)"] --> b["Print #quot;hello#quot;"]
Inputs (sensors, buttons) and Outputs (motors, LEDs, buzzer), and a Pico node in the middle.hardware class and apply it to every physical part.linkStyle to make every link going to an output thick and coloured.subgraph has a matching end, and no node is called endWhy: Mermaid uses end to know where the group finishes
linkStyle colours the wrong arrowWhy: Links are numbered by position, and a --> b --> c counts as two links
:::classNameQuick quiz 1 of 3
Why does start --> end break a flowchart?
End or a different ID such as finish([End]).Quick quiz 2 of 3
How do you apply the class hardware to a node called motor?
::: attaches a class to one node; the class line can apply it to several at once.Quick quiz 3 of 3
In a --> b --> c, which number does linkStyle use for the arrow from b to c?
a --> b is 0 and b --> c is 1.
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments