Class 4 · Find the target¶
Your catapult aims and sets its range, but you're still the one finding the cup. Today the ultrasonic sensor rides on the turret. The turret sweeps from side to side like a radar, measures the distance at each angle, and turns to face the closest thing it found.
By the end of class
Someone else places a cup, you press the scan button, and the turret turns to point at it.
New parts today¶
| Part | What it's for |
|---|---|
| HC-SR04 ultrasonic sensor | Measures the distance to whatever is in front of the turret |
| Pushbutton | The scan button (it becomes the auto button next class) |
Build it¶
- Mount the sensor on the turret, low and at the front, pointing exactly where the catapult throws. If the sensor points a little left, every shot goes a little right.
- Slack again. The sensor's four wires need the same loose loop as the servo wires.
-
Wire it, everything unplugged:
From To HC-SR04 VCC Logic rail +HC-SR04 GND Logic rail −HC-SR04 TRIG Pin 5 HC-SR04 ECHO Pin 6 Scan button, one leg Pin A1 Scan button, diagonally opposite leg GND
The analog pins work as ordinary digital pins too: pinMode(A1, INPUT_PULLUP) and digitalRead(A1) work just like pin 2.
You already have the sensor code: readDistanceCm() from the dispenser project. Paste it above setup().
Tool: for repeats with a counter
A for loop runs the same lines again and again while a counter changes. It has three parts: where the counter starts, when to keep going, and how it changes each time.
Here's a Celsius-to-Fahrenheit chart, from 0 °C to 100 °C in steps of 10:
#include <Arduino.h>
void setup() {
Serial.begin(9600);
for (int c = 0; c <= 100; c += 10) {
int f = c * 9 / 5 + 32;
Serial.print(c);
Serial.print(" C = ");
Serial.print(f);
Serial.println(" F");
}
}
void loop() {
}
int c = 0: start at 0.c <= 100: keep going whilecis 100 or less.c += 10: add 10 each time.
It prints 11 lines: 0, 10, 20 ... 100. Change it to c = -40; c <= 40 and you'll find the one temperature that's the same in both.
A for inside a for is fine too. This prints a bar of # signs, one for every 10 units:
Tool: keep the best so far
To find the biggest (or smallest) of a lot of numbers, you don't need to keep them all. Keep the best so far, and replace it whenever you see something better.
Roll two dice ten times. Which roll was highest?
#include <Arduino.h>
void setup() {
Serial.begin(9600);
randomSeed(analogRead(A5));
int best = 0; // worse than any real roll
int bestTry = 0;
for (int t = 1; t <= 10; t++) {
int dice = random(1, 7) + random(1, 7);
Serial.print("Try ");
Serial.print(t);
Serial.print(": ");
Serial.println(dice);
if (dice > best) { // better than anything so far?
best = dice;
bestTry = t;
}
}
Serial.print("Best was ");
Serial.print(best);
Serial.print(", on try ");
Serial.println(bestTry);
}
void loop() {
}
For the smallest, start best at something huge (like 999) and use < instead of >.
Mission 1 · Trust the sensor
With the turret straight ahead, print the distance a few times a second. Put a cup at 50 cm, then at 150 cm, measured with the tape from the sensor.
Done when
The Serial Monitor agrees with the tape measure within a few centimetres at both distances.
Mission 2 · Radar
When the scan button is pressed, sweep from −90° to +90° in steps of 10°. At each angle: move there, wait a moment for it to settle, read the distance, and print one line with the angle, the distance and a bar of # signs. Then go back to straight ahead.
The sensor needs about 60 ms between readings, or it hears the echo from the last one.
Done when
Put one cup about 1 m away, off to the side. Your radar shows a short bar at the cup's angle (within 10°), and long bars or 999 everywhere else.
Mission 3 · Turn to the closest
During the sweep, keep the closest distance so far and the angle where you saw it. After the sweep, turn to that angle instead of straight ahead. Ignore readings that are too far to matter: anything past your longest shot from Class 3 is not a target.
Done when
Someone from another group places a cup anywhere in front of the catapult (nothing else nearby). You press scan, the turret points at it. Then arm, set the range with stopAngleFor(), and take the shot. Do it for three different cup spots.
The sensor sees walls too
The HC-SR04 hears echoes from table legs, bags and walls. Clear the area in front of the catapult, and keep the cups closer than anything else.
If you finish early¶
- The sensor's beam is wide, so a cup shows up at several angles in a row. Point at the middle of that run instead of the first angle: remember where the short readings start and where they stop.
- Sweep in 5° steps. Better aim, slower sweep. Is it worth it?
- Print the radar sideways, so it looks like a picture of the room.
Sign-off with Dr. Alvear
- The sensor matches the tape measure at 50 cm and 150 cm
- The radar prints a dip at the cup's angle
- The turret turns to a cup someone else placed, three times
- A shot at the cup after each scan
- Build log updated (photo + two sentences)