KevsRobots Learning Platform
By Kevin McAleer, 5 Minutes
A flowchart shows steps. A sequence diagram shows conversations: who sends a message to whom, and in what order. Time runs from top to bottom.
They’re brilliant for anything involving communication: a Pico asking a sensor for a reading over I2C, a phone app sending commands to a robot over Wi-Fi, or a web page talking to a server.
sequenceDiagram
participant Pico
participant Sensor as BME280 sensor
Pico->>Sensor: Request temperature
Sensor-->>Pico: 21.5 °C
Here’s what it draws:
sequenceDiagram
participant Pico
participant Sensor as BME280 sensor
Pico->>Sensor: Request temperature
Sensor-->>Pico: 21.5 °C
participant adds a box at the top with a lifeline running down from it. as gives it a longer display name.From->>To: text.Use actor instead of participant to draw a little stick person, handy for showing a human pressing a button.
| Arrow | Meaning |
|---|---|
->> |
Solid line with arrowhead: a request or command |
-->> |
Dotted line with arrowhead: a reply |
-> |
Solid line, no arrowhead |
-x |
Solid line ending in a cross: a message that failed or was lost |
-) |
Open arrowhead: a message sent without waiting for a reply |
The usual convention is solid arrows for requests and dotted arrows for replies.
Activation bars show when a participant is busy. Add + after the arrow to switch one on and - to switch it off. Notes add comments, and autonumber numbers every message:
sequenceDiagram
autonumber
actor Kev
participant App as Phone app
participant Robot as Pico W robot
Kev->>App: Taps "Forward"
App->>+Robot: POST /move?dir=forward
Note right of Robot: Motors on for 1 second
Robot-->>-App: 200 OK
App-->>Kev: Shows "Moving"
Here’s what it draws:
sequenceDiagram
autonumber
actor Kev
participant App as Phone app
participant Robot as Pico W robot
Kev->>App: Taps "Forward"
App->>+Robot: POST /move?dir=forward
Note right of Robot: Motors on for 1 second
Robot-->>-App: 200 OK
App-->>Kev: Shows "Moving"
Notes can go left of, right of, or over one or two participants: Note over App,Robot: Wi-Fi.
Real conversations repeat and branch. Wrap messages in a loop, alt/else or opt block, and finish each with end:
sequenceDiagram
participant Pico
participant Sonar as HC-SR04
participant Motors
loop Every 100 ms
Pico->>Sonar: Trigger pulse
Sonar-->>Pico: Echo time
alt Closer than 10 cm
Pico->>Motors: Stop
else Clear ahead
Pico->>Motors: Forward
end
end
opt Button pressed
Pico->>Motors: Spin
end
Here’s what it draws:
sequenceDiagram
participant Pico
participant Sonar as HC-SR04
participant Motors
loop Every 100 ms
Pico->>Sonar: Trigger pulse
Sonar-->>Pico: Echo time
alt Closer than 10 cm
Pico->>Motors: Stop
else Clear ahead
Pico->>Motors: Forward
end
end
opt Button pressed
Pico->>Motors: Spin
end
loop - repeatsalt / else - one choice or another, like if / else in Pythonopt - something that only happens sometimes, like an if with no elseButton, Pico and LED participants.autonumber, and activation bars on the Pico.loop for “every minute” and an alt for “temperature above 30 °C” that also sends an alert.pico and Pico are differentWhy: Any new name in a message creates a new participant automatically
loop or altend, including nested onesWhy: Mermaid needs to know where each block finishes
+ with a - on the replyQuick quiz 1 of 3
Which way does time run in a sequence diagram?
Quick quiz 2 of 3
Which block is like an if / else in Python?
alt shows one choice or another. opt is for something that may or may not happen.Quick quiz 3 of 3
What does -->> usually show?
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments