Class 1 · Fire it¶
Today you build the catapult and make a servo fire it. By the end, a button fires it, but only when the arming switch is on.
By the end of class
You arm the catapult, press the button, and hit a cup 1 m away. It's on video.
New parts today¶
| Part | What it's for |
|---|---|
| Catapult kit (or printed catapult) | The arm, the frame, the rubber bands |
| MG90S servo | The latch: holds the arm down, lets it go |
| Toggle switch | Arming switch |
| LED + 470 Ω | Arming light: on means armed |
| Pushbutton | Fire button |
| 10 kΩ pot | For finding the servo angles today; it becomes the aiming knob next class |
| 5 V supply | Power for the servo (the motor rail) |
Build it¶
- Build the catapult following its instructions. Leave the rubber bands off until step 4.
- Make a latch. When you cock the arm, something has to hold it down: a stiff wire, a wooden skewer or a small hook that sits over the arm. Mount the servo so its horn can pull that pin out of the way. Hot glue, a zip tie or two small screws all work. Ask Dr. Alvear to look before you glue.
-
Wire it, USB unplugged:
From To Arduino 5V Logic rail +Arduino GND Logic rail −5 V supply +Motor rail +5 V supply −Motor rail −Logic rail −Motor rail −(the shared ground)100 µF, long leg → motor rail +short leg → motor rail −, near the servoLatch servo red / brown Motor rail +/−Latch servo signal (orange) Pin 9 Pot outer legs Logic rail +and−Pot middle leg A0 Fire button, one leg Pin 2 Fire button, diagonally opposite leg GND Arming switch, middle terminal Pin 3 Arming switch, one outer terminal GND Pin 4 → 470 Ω → LED long leg LED short leg → GND -
Rubber bands on only after the latch works.
Two supplies, one ground
The servo gets its power from the 5 V supply, not the Arduino. The two − rails must be joined, or the servo won't understand the signal. The two + rails must never be joined.
The toggle switch is just a button that stays where you put it. With INPUT_PULLUP, on connects pin 3 to GND, so on reads LOW, the same as a pressed button.
Your platformio.ini:
Tool: a servo tester
Your latch needs two angles: hold (the pin is in, the arm stays down) and release (the pin is out). You find them by experiment. This program turns the knob into a servo angle and prints it, so you can try angles without uploading again and again.
#include <Arduino.h>
#include <Servo.h>
Servo tester;
void setup() {
tester.attach(9);
Serial.begin(9600);
}
void loop() {
int knob = analogRead(A0); // 0 to 1023
int angle = map(knob, 0, 1023, 0, 180); // 0 to 180 degrees
tester.write(angle);
Serial.println(angle);
delay(100);
}
It's a tool, not your project. Keep it in a file somewhere; you'll use it again for the second servo in Class 3.
Mission 1 · Find hold and release
Upload the servo tester. Turn the knob slowly until the pin sits firmly over the arm: that's hold. Turn until the pin is clear and the arm is free: that's release. Write both numbers down. Stay a few degrees away from 0 and 180, where the servo hums against its end stop.
Done when
You have two numbers in your build log. At hold the cocked arm stays down; at release it flies.
Tool: && means both
Sometimes one thing should happen only if two things are true. && means "and": the whole test is true only when both sides are.
Here's a car that only starts if you press start while your foot is on the brake:
#include <Arduino.h>
const int START_BTN = 2;
const int BRAKE_PEDAL = 3;
const int ENGINE_LED = 13;
void setup() {
pinMode(START_BTN, INPUT_PULLUP);
pinMode(BRAKE_PEDAL, INPUT_PULLUP);
pinMode(ENGINE_LED, OUTPUT);
}
void loop() {
bool start = (digitalRead(START_BTN) == LOW); // pressed reads LOW
bool brake = (digitalRead(BRAKE_PEDAL) == LOW);
if (start && brake) {
digitalWrite(ENGINE_LED, HIGH); // engine on
}
}
Press start without the brake: nothing. Brake without start: nothing. Both: the engine starts. There's also ||, which means "or": true if either side is.
Mission 2 · Armed or not
Write your catapult program, starting from an empty main.cpp:
- Use
const intnames for every pin and for yourHOLDandRELEASEangles. - In
setup(), move the latch toHOLD. - The arming LED is on when the switch is on, and off when it's off.
- Pressing fire while armed moves the latch to
RELEASE, waits about a second, then goes back toHOLD. - Pressing fire while not armed does nothing.
- One press fires once, even if you hold the button. Use your
wasPressedpattern.
Done when
Show Dr. Alvear: disarmed, press fire, nothing moves. Armed, the LED is on, press fire, the latch opens once and closes again.
Mission 3 · First shot
Put a cup 1 m from the catapult, measured with the tape. Aim by turning the whole catapult with your hands. Disarm, cock, load, step back, arm, fire. Change rubber bands or move the cup line if you're always short or long, but keep the cup at 1 m.
Done when
You hit the cup (ball in it, or cup knocked over), and it's on video.
If you finish early¶
- Make it beep: a short
tone()on fire. You'll wire the speaker for real in Class 5, but nothing stops you now (pin 7, through 220 Ω and 100 Ω). - Blink the arming LED fast for a second after each shot, so everyone knows it just fired.
- Count shots and print the count to the Serial Monitor.
- Try three kinds of ammo. Which flies straightest?
Sign-off with Dr. Alvear
- Hold and release angles written down
- Disarmed: fire does nothing. Armed: LED on, one press fires once
- Video of a hit at 1 m
- Build log updated (photo + two sentences)