Class 5 · Pictures from the laptop¶
Your robot draws anything you can type in as a list of points, but the list lives in the Uno's small memory and you have to work out every coordinate by hand. Today the laptop takes over that part. A Python script turns a line of text or a drawing file into points and sends them one at a time over USB. Your Arduino code listens, moves, and answers.
By the end of class
You type a word on the laptop, or pick a simple SVG drawing, and the robot draws it.
New parts today¶
Nothing new on the robot. On the laptop: Python, the pyserial package, and Claude Code.
The protocol¶
A protocol is the set of rules two programs agree on so they can talk. Yours is tiny. The laptop sends one command per line; the robot does it and replies ok when it's finished. The laptop doesn't send the next command until it gets ok. That's what keeps the laptop from flooding the Uno with points faster than the motors can draw them.
| Laptop sends | The robot | Replies |
|---|---|---|
M 120.5 300 |
Draws or moves in a straight line to (120.5, 300) in mm, with the pen as it is | ok when it gets there |
U |
Lifts the pen | ok |
D |
Lowers the pen | ok |
H |
Lifts the pen and goes back to the home cross | ok |
| anything else | Does nothing | err |
When the program starts, the robot sends ready once. Speed is 115200 baud, and every line ends with a newline.
Opening the port resets the Uno
When a program on the laptop opens the Uno's USB port, the Uno restarts. Your code then believes the gondola is on the home cross, wherever it really is. So: park the gondola on the home cross before you run the script, hold it during the restart, and end every drawing with H so it's home for the next one.
Where Claude Code fits¶
The Arduino side is yours: reading the commands, the math, the motors, the pen. You've built all of it already; today you add the part that listens.
The laptop side is where Claude Code helps. Turning an SVG file or a font into points, and writing Python that talks over a serial port, are new skills that aren't the point of this course. Ask Claude Code for the script, give it your protocol table above, and read what it writes. You decide the rules; the script follows them.
Build it¶
Add two lines to platformio.ini so the Serial Monitor shows what you type and ends each line with a newline:
Tool: reading a command from Serial
The Serial Monitor sends each character as you type it, not a whole line at once. So the code collects characters into a String until a newline arrives, then handles the whole line. This example is a desk lamp on pin 3 that takes commands: B 128 sets the brightness, O turns it off.
const int LAMP = 3;
String line = "";
void handle(String cmd) {
cmd.trim(); // remove spaces and \r at the ends
if (cmd.length() == 0) {
return;
}
char letter = cmd.charAt(0);
if (letter == 'B') {
int level = cmd.substring(2).toInt(); // everything after "B "
analogWrite(LAMP, constrain(level, 0, 255));
Serial.println("ok");
} else if (letter == 'O') {
analogWrite(LAMP, 0);
Serial.println("ok");
} else {
Serial.println("err");
}
}
void setup() {
Serial.begin(115200);
pinMode(LAMP, OUTPUT);
Serial.println("ready");
}
void loop() {
while (Serial.available() > 0) {
char c = Serial.read();
if (c == '\n') {
handle(line);
line = "";
} else {
line += c;
}
}
}
substring(2)is the text from position 2 to the end (positions start at 0).toInt()andtoFloat()turn text into a number. They give 0 if the text isn't a number, so check your values.- For two numbers, find the space between them with
indexOf(' ', 2), then take the text before and after it.
Mission 1 · The robot listens
Add the protocol to your robot's code: ready at start, then M, U, D and H, each replying ok only when it has finished, and err for anything else. Keep your goTo, lineTo, penUp and penDown exactly as they are; the new code just calls them. Use constrain() so an M outside your safe area can't send the pen off the board. Test it by typing commands in the Serial Monitor.
Done when
Typing D, M 200 250, M 300 250, U, H in the Serial Monitor draws one 100 mm line and returns home, with an ok after each command, and hello gets err.
Mission 2 · The bridge, with Claude Code
Close the Serial Monitor (only one program can use the port at a time). Find the Uno's port name with pio device list. Ask Claude Code for a Python script that:
- opens the port at 115200 baud, waits for
ready, and sends one command at a time, waiting forokeach time; - turns a line of text into strokes using a single-stroke font (letters drawn as lines, not outlines), lifts the pen between strokes, and scales the text to fit a box on the board you give it in mm;
- stops and prints the error if it gets
err, and sendsHat the end.
Read the script before you run it. Make sure the commands it sends are exactly your protocol and that its coordinates match yours (origin at the top-left pulley, y down, mm). Run it with a short word first.
Done when
A word of at least four letters, typed on the laptop, is drawn readably on the board, and the gondola ends on the home cross.
Mission 3 · A drawing from a file
Extend the script (with Claude Code) to read an SVG file, turn its lines and curves into points a couple of millimeters apart, and scale it to fit your safe area. Start with a simple line drawing you made yourself or are allowed to use: outlines, not filled shapes or photos.
Done when
An SVG drawing of your choice comes out on the board, recognizable and the right way up. Photo next to the original in the build log.
If you finish early¶
- Make the script print how far along it is (points sent of total) and how long the drawing will take.
- Have the script skip pen-up moves shorter than a millimeter or two; drawings get much faster.
- Add a command of your own to the protocol, for example one that changes the drawing speed, and implement both sides.
- Draw your Class 3 square from the laptop and check it's as straight as before.
Sign-off with Dr. Alvear
- Arduino code (yours) speaks the protocol:
ready,M,U,D,H,ok,err - Commands tested by hand in the Serial Monitor
- Python bridge draws a typed word; you can say what each part of it does
- An SVG drawing on the board
- Build log updated (photo + two sentences)