Skip to content

Class 5 · Automatic

Every piece works: fire, aim, range, find. Today you put them in order, so one button does the whole thing: sweep, find the cup, turn, set the range, beep a countdown, fire. You don't touch it once you've pressed the button.

By the end of class

Someone else places three cups. Your catapult, untouched, hits at least one of them.

New parts today

Part What it's for
Speaker + 220 Ω + 100 Ω The countdown beeps before every automatic shot

Build it

  1. Wire the speaker, everything unplugged:

    From To
    Pin 7 220 Ω → 100 Ω → speaker
    Speaker, other wire GND
  2. That's the last of the wiring. Check every wire against the pin map.

Tool: a function that can say \"stop\"

A function can hand back true or false to say whether it finished. And return ends the function right away, even in the middle of a loop.

Here's a microwave that counts down, but stops the moment the door opens:

#include <Arduino.h>

const int DOOR = 3;      // door switch: closed reads LOW
const int BEEPER = 7;

// Cook for some seconds. Returns true if it finished,
// false if the door was opened.
bool cook(int seconds) {
  for (int s = seconds; s > 0; s--) {
    Serial.println(s);
    if (digitalRead(DOOR) == HIGH) {   // door open?
      noTone(BEEPER);
      return false;                    // stop, right now
    }
    tone(BEEPER, 200, 100);            // a quiet hum each second
    delay(1000);
  }
  tone(BEEPER, 1000, 500);             // ding
  return true;
}

void setup() {
  pinMode(DOOR, INPUT_PULLUP);
  Serial.begin(9600);

  if (cook(5)) {
    Serial.println("Food's ready");
  } else {
    Serial.println("Stopped: door open");
  }
}

void loop() {
}

The code that calls cook() decides what to do with the answer. That's how a long job can be cancelled from inside.

Mission 1 · Countdown

Write bool countdown(). It beeps three short beeps a second apart, then one long, higher beep. If the arming switch goes off at any point, it stops and returns false. Otherwise it returns true.

Done when

Run it with the switch on: three beeps and a long one. Run it again and flip the switch off after the second beep: it goes quiet and the Serial Monitor says it was cancelled.

Mission 2 · One button

Change the scan button into the auto button. When it's pressed and the catapult is armed:

  1. Sweep and find the closest target. If there isn't one, print "no target" and stop.
  2. Turn to it.
  3. Set the stop bar with stopAngleFor().
  4. countdown(). If it returns false, don't fire.
  5. Fire, then put the latch back to hold.

Print a line at each step, so when something goes wrong you can see where.

Write each step as its own function (fire(), turnTo(), and so on) and the button's code becomes five short lines. Most of them you already wrote; this is mostly moving code into functions.

Two things to check:

  • Where you measure from. Your range table measured from the turntable's center. The sensor is in front of the center, so add that gap to the sensor's reading. The sensor also sees the near edge of the cup; you want the middle, so add half a cup.
  • Manual still works. The knob and the fire button work exactly as before when you're not running the automatic shot.

Done when

One cup, placed by you. Arm, press auto, step back. It sweeps, turns, beeps and fires, and the Serial Monitor shows all five steps.

Before every automatic run

Look in front of the catapult. Nobody within its sweep, nobody downrange. The turret can turn 90° either way before it shoots.

Mission 3 · Three cups

Someone from another group places three cups anywhere in front of the catapult, within your range. You don't touch the turret, the knob or the cups. Three automatic runs: between runs, disarm, cock, load, step back, arm, press auto. It may go for the same cup more than once; that's fine.

Done when

At least one hit out of three runs, with the catapult untouched. Video all three.

If you finish early

  • After a hit, the cup falls over and the next sweep finds the next one. Does it?
  • Light the arming LED differently during the countdown: blink with each beep.
  • Make the countdown last longer for far cups, shorter for close ones.
  • Time a full run. Where does most of the time go? Make that part faster.

Sign-off with Dr. Alvear

  • Countdown before every automatic shot; disarming cancels it
  • Auto only runs when armed
  • The Serial Monitor shows each step of a run
  • Video: at least one hit out of three cups placed by someone else
  • Build log updated (photo + two sentences)