Class 3 · Lines and shapes¶
The pen goes where you send it. But if you ask for a point across the board, the path it takes to get there bends. Today you make straight lines out of many short moves, add the servo that lifts the pen, and draw your first real shapes.
By the end of class
The robot draws a square, a circle and your group's initials, with no lines between them.
New parts today¶
| Part | What it's for |
|---|---|
| Servo (from your kit), mounted on the gondola | Lifts the pen off the board and puts it back |
| Servo extension cable, 1–2 m | Reaches the moving gondola from the shield |
Build it¶
The servo goes on pin 9. The shield has no pin labeled for a servo, so you borrow one: the X limit switch pins (usually labeled X− and X+, both wired to D9) are free. This robot has no limit switches, so nothing else uses pin 9. The Servo library stops analogWrite on pins 9 and 10, which you don't use either.
| From | To |
|---|---|
| Servo signal (orange or yellow) | Shield X− (or X+) limit pin = D9 |
| Servo + (red) | A 5V pin on the shield |
| Servo − (brown or black) | A GND pin on the shield |
- 24 V off and USB unplugged before you wire.
- Find a 5V pin and a GND pin on your shield by reading the labels printed on it. The limit headers have signal and ground pins; 5V is usually elsewhere on the board. If you can't find a 5V pin, show Dr. Alvear before you improvise.
- Plug the servo into the extension cable, and run the cable from the top middle of the frame down to the gondola with enough slack to reach every corner. A cable that pulls on the gondola pulls the pen off its point.
- Design the lift so the servo arm pushes against the board and tips the pen tip away, or raises the marker in its holder. Either works. Test it by hand before you print the final version.
If the Uno resets when the servo moves
The servo runs from the Uno's 5 V. A micro servo lifting a pen is a light load, but if the Uno resets or the Serial Monitor drops out when the pen lifts, tell Dr. Alvear. The fix is a separate 5 V supply for the servo with its ground joined to the Uno's.
Mission 1 · Pen up, pen down
Write penUp() and penDown() using the Servo library. Find the two angles by trying them: down is the smallest lift that still lets the marker touch firmly; up is clear of the board by a few millimeters. Add a short delay in each so the pen finishes moving before the motors start.
Done when
penDown(), goTo a point, penUp(), goTo another point draws exactly one line, with no mark from the second move.
Why does a single goTo draw a curve?
MultiStepper makes both motors arrive together, each at a constant speed. That makes the belt lengths change evenly, not the pen's x and y. Belt length isn't a straight-line function of position (it has a square root in it), so the pen sweeps along a curve. Over 5 mm the curve is too small to see. Over 300 mm it's obvious.
Tool: for loops
A for loop repeats a block a set number of times and counts as it goes. This one blinks an LED ten times and prints the count:
for (int i = 0; i < 10; i++) {
Serial.println(i); // 0, 1, 2 ... 9
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
}
The three parts in the parentheses are: start (int i = 0), keep going while (i < 10), and after each pass (i++, add one). Use i <= 10 if you want the loop to include 10.
Tool: splitting a move into small steps
To go from a value a to a value b in n equal pieces, piece number i is at
a + (b − a) × i ÷ n
When i is 0 you're at a; when i is n you're at b. Here it sweeps a servo smoothly from 20° to 160° in 50 small moves, instead of jumping:
float from = 20.0;
float to = 160.0;
int pieces = 50;
for (int i = 1; i <= pieces; i++) {
float angle = from + (to - from) * i / pieces;
myServo.write(angle);
delay(20);
}
Do the same thing to x and to y at once, and the points land on a straight line between two points.
Mission 2 · Straight lines and a square
Write lineTo(float x, float y): it remembers where the pen is now, splits the line to (x, y) into short pieces (about 2 mm each; work out n from the line's length), and calls goTo for each piece. Use it to draw a 100 mm square.
Done when
Lay a ruler along each side of the square: no side wanders more than 2 mm from straight, and the last corner closes on the first.
Tool: points on a circle (sin and cos)
A point at angle θ on a circle of radius r, centered at (cx, cy), is at x = cx + r × cos θ, y = cy + r × sin θ. The Arduino's sin() and cos() take the angle in radians: a full turn is 2π, which you can write as TWO_PI. This prints where the 12 hour marks go on a clock face 80 mm across:
for (int hour = 0; hour < 12; hour++) {
float angle = TWO_PI * hour / 12.0;
float x = 100.0 + 40.0 * cos(angle);
float y = 100.0 + 40.0 * sin(angle);
Serial.print(x);
Serial.print(", ");
Serial.println(y);
}
More points make a smoother circle.
Mission 3 · A circle and your initials
Draw a circle 100 mm across next to your square. Then draw your group's initials, about 80 mm tall, built from straight lines (and arcs if you like). The pen lifts between letters.
Done when
Square, circle and initials are on the board in one run, with no stray lines between them. Photo in the build log.
If you finish early¶
- Make
lineTowork out its own number of pieces from the length, so short lines don't get 50 pieces and long ones don't get too few. - Draw the same square at the top of your safe area and at the bottom. Does it come out the same size? If not, measure W and the home point again.
- Try a spiral: a circle where the radius grows a little with every point.
- Draw a star with a
forloop.
Sign-off with Dr. Alvear
- Servo on pin 9 lifts and lowers the pen on command
-
lineTosplits lines into shortgoTomoves - Square (straight within 2 mm), circle and initials in one run, no stray lines
- Build log updated (photo + two sentences)