Class 3 · Range¶
Your catapult aims left and right. Today it learns how far to throw. A second servo moves a stop bar: the arm hits it early for a short shot, or late for a long one. You measure what each setting does, then use your own table to hit a cup at a distance you're given.
By the end of class
Dr. Alvear names a distance, you pick the stop-bar setting from your table, and you hit the cup.
New parts today¶
| Part | What it's for |
|---|---|
| Second MG90S servo | Moves the stop bar |
| A stick or stiff strip for the bar | What the arm hits |
| Tape measure, phone | Measuring where each shot lands |
Build it¶
- Find where the arm stops now. Most kits have a fixed crossbar that stops the arm. Your servo's bar replaces it, so the fixed one has to come off or move out of the way. Ask Dr. Alvear before you cut anything.
- Mount the servo on the catapult frame so a bar on its horn swings into the arm's path. At one end of the servo's travel, the arm hits the bar early. At the other end, the arm swings almost all the way.
- Make it strong. The arm slams into the bar on every shot. Glue or screw the servo down well, and fix the bar to the horn with a screw, not just tape.
-
Wire it, everything unplugged:
From To Stop-bar servo red / brown Motor rail +/−Stop-bar servo signal Pin 10
Tool: turn a table into code
You'll measure a table, and your code needs to read between its rows. map() does that, one pair of rows at a time.
Say you measured a garden sprinkler: turn the valve to an angle, see how far the water sprays.
| Valve angle | Spray distance |
|---|---|
| 20° | 100 cm |
| 40° | 180 cm |
| 60° | 300 cm |
You want the water to reach 240 cm. That's between the 40° row and the 60° row. map() finds the valve angle between them:
#include <Arduino.h>
// Which valve angle sprays this far? From our table.
int valveFor(int distanceCm) {
int angle;
if (distanceCm <= 180) {
angle = map(distanceCm, 100, 180, 20, 40); // rows 1 and 2
} else {
angle = map(distanceCm, 180, 300, 40, 60); // rows 2 and 3
}
return constrain(angle, 20, 60); // never outside the table
}
void setup() {
Serial.begin(9600);
Serial.println(valveFor(240)); // prints 50
Serial.println(valveFor(140)); // prints 30
}
void loop() {
}
Each if picks the two rows the distance falls between, and map() reads between them. Five rows means four map() lines.
The quick version is one line, map(distanceCm, 100, 300, 20, 60), which draws a straight line from the first row to the last. It's close if your graph is nearly straight, and off if it curves.
Mission 1 · Find the range of the bar
Use your servo tester from Class 1 with pin 9 changed to 10. Find the angle where the arm hits the bar earliest (shortest shot) and the angle where it swings the farthest and still hits the bar (longest shot). Pick five angles evenly spread between those two.
Done when
Your five angles are in your build log, and one shot at each end lands at clearly different distances.
Mission 2 · The range experiment
Add the stop-bar servo to your catapult program, with const int STOP_ANGLE set in setup(). For each of your five angles: change STOP_ANGLE, upload, and fire three shots straight ahead. Measure from the center of the turntable to where the ball first lands. Record the shots in slow motion on your phone so you can see the exact spot.
Keep everything else the same: same ammo, same rubber bands, cocked the same way.
Write your prediction before you start, then fill in the table:
| Stop angle | Shot 1 | Shot 2 | Shot 3 | Average |
|---|---|---|---|---|
Graph it: stop angle across the bottom, average distance up the side. Paper or a spreadsheet.
Done when
The table and the graph are in your build log, with two sentences: did a bigger angle always throw farther? Why weren't your three shots the same?
Mission 3 · Called shot
Write int stopAngleFor(int distanceCm) from your table, like valveFor() above. Dr. Alvear puts a cup somewhere in your range and tells you the distance. Your code prints the angle it picked, moves the stop bar there, and you aim with the knob and fire.
If a bigger angle throws shorter on your catapult, that's fine: map() works with the numbers either way round. Just keep the rows in order of distance.
Done when
You hit the cup at the distance Dr. Alvear chose, using the angle your function picked. Video it.
If you finish early¶
- Try a cup at a distance halfway between two rows. How close does it land?
- Add a fourth and fifth shot to your best row. Does the average change?
- Does a pom-pom fit the same table as a ping-pong ball? (If not, pick one ammo and stick with it.)
Sign-off with Dr. Alvear
- Stop bar mounted firmly; it doesn't move when the arm hits it
- Table of 5 × 3 shots, with averages, and a graph
-
stopAngleFor()picks an angle, and the called shot hits (video) - Build log updated (photo + two sentences)