class: center, middle, inverse, title-slide .title[ # Dispenser: Same Code, Cardboard Prototype ] .subtitle[ ## Programmable Electronics — Unit 3 ] .date[ ### Class 19 ] --- <style> .code-tight .remark-code { font-size: 14px; line-height: 1.2; } .code-tighter .remark-code { font-size: 12.5px; line-height: 1.15; } .remark-slide-content table { width: 100%; } .check { border: 2px solid #364F6B; border-radius: 8px; padding: 8px 16px; margin-top: 10px; } </style> # Learning Targets .lt-box[ - Get every group's dispenser running the same program, with the real sensor. - Choose a sensor threshold from measurements, not a guess. - Build a cardboard prototype of your mechanism, and test it with real stuff. - Write your machine's rules in plain English, ready to turn into code. ] .eu-label[Essential Understanding:] A prototype is a question you ask with cardboard: *will this work?* It's cheap to build so it can fail cheaply. Every jam you find today is one you won't find on demo day. --- # Agenda .agenda-box[ 1. **Everyone on the same code:** wiring check, the full program, a sign-off 2. **Tune** the sensor to your target 3. **Build** the cardboard prototype 4. **Test** it with real stuff, and count what goes wrong 5. Write your machine's **rules** in plain English ] .center[ By the end of class: a machine that **senses on its own** and gives **one portion per visit**. Next class you give it more rules. ] --- class: small-font # Step 1: wiring check <img src="assets/schematics/dispenser-ultrasonic.svg" alt="The base circuit plus the ultrasonic sensor: VCC to 5V, TRIG to pin 5, ECHO to pin 6, GND to ground. The pin 7 button stays as an owner button." style="width:100%;max-height:250px;"/> .pull-left.w48[ **Unplug the USB**, then check every wire against this list: - **Servo:** signal → **pin 9**, red → **+**, brown/black → **−** - **100 µF** across the rails, **short leg to −** - **Button:** **pin 7** → **−** (the owner button) ] .pull-right.w48[ - **Sensor:** VCC → **+**, Trig → **pin 5**, Echo → **pin 6**, GND → **−** - **Arduino 5V → +** and **GND → −**. Not the battery pack. ] --- class: small-font # Step 2: the full program .pull-left.w50.code-tighter[ ```cpp #include <Arduino.h> #include <Servo.h> Servo gate; // the servo that opens the gate const int CLOSED = 0; // angle that blocks the opening const int OPEN = 90; // angle that lets food fall const int PORTION = 500; // ms the gate stays open const int TRIG = 5; // sensor: chirp const int ECHO = 6; // sensor: echo const int NEAR = 10; // cm: closer = "something there" bool wasThere = false; // there last time through loop()? long readDistanceCm() { digitalWrite(TRIG, HIGH); // a 10 µs chirp delayMicroseconds(10); digitalWrite(TRIG, LOW); long us = pulseIn(ECHO, HIGH, 30000); // wait for the echo if (us == 0) return 999; // no echo: nothing near return us / 58; // µs → cm } bool somethingThere() { // is a cat, a bowl, a hand there? long cm = readDistanceCm(); Serial.print("distance: "); Serial.println(cm); return cm < NEAR; } ``` ] .pull-right.w46.code-tighter[ ```cpp void dispense() { Serial.println("Dispensing!"); gate.write(OPEN); delay(PORTION); gate.write(CLOSED); delay(300); // give the gate time to close } void setup() { gate.attach(9); gate.write(CLOSED); pinMode(7, INPUT_PULLUP); // owner button pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT); Serial.begin(9600); Serial.println("Dispenser ready."); } void loop() { bool there = somethingThere() || digitalRead(7) == LOW; if (there && !wasThere) { // just arrived? dispense(); } wasThere = there; // remember it delay(60); // the sensor needs a rest } ``` **Replace everything** in `src/main.cpp` with this, even if you finished the sensor last class. Then put **your own** `CLOSED`, `OPEN` and `PORTION` back in. .font-small[ Copy it from **[here](code/dispenser-sensor.cpp)** instead of typing. `platformio.ini` still needs `lib_deps = arduino-libraries/Servo`. ] ] --- class: small-font # How it works: the parts The program is **sense → decide → act**, and each function has one job. | Part | What it does | |---|---| | `const int CLOSED`, `OPEN`, `PORTION`, `NEAR`... | **Your numbers**, named once at the top. Change one here and it changes everywhere. | | `bool wasThere` | The program's **memory**: was something there last time? It lives outside every function, so it isn't forgotten. | | `readDistanceCm()` | **Sense.** Pin 5 makes the sensor chirp. `pulseIn` times how long pin 6 stays HIGH until the echo comes back. Sound goes there **and back**, about 58 µs per cm. No echo? It answers 999: "nothing near." | | `somethingThere()` | Turns the distance into a **yes or no**: closer than `NEAR`? It also prints every distance, so you can watch it. | | `dispense()` | **Act.** One portion: open the gate, wait `PORTION` ms, close it, wait for it to shut. | | `setup()` | Runs **once** at power-on: attach the servo, close the gate, set up the pins, start the Serial Monitor. | | `loop()` | **Decide.** Runs over and over, forever. Next slide. | --- class: small-font # How it works: loop(), about 15 times a second .pull-left.w50.code-tight[ ```cpp void loop() { bool there = somethingThere() || digitalRead(7) == LOW; if (there && !wasThere) { // just arrived? dispense(); } wasThere = there; // remember it delay(60); // the sensor needs a rest } ``` `||` means **or**. `&&` means **and**. `!` means **not**. ] .pull-right.w46[ Every trip through `loop()`: 1. **Is something there?** Near the sensor, **or** the button pressed (pin 7 reads LOW). 2. **Did it just arrive?** It's there now **and** it was **not** there last trip. Only then, dispense. 3. **Remember** this trip's answer for the next one. 4. **Rest** 60 ms, and go again. | `wasThere` | `there` | So... | |---|---|---| | false | **true** | **just arrived: dispense** | | true | true | still here: do nothing | | true | false | just left: do nothing | | false | false | nobody: do nothing | .font-small[ That's why it's **one portion per visit**: to get another, the target has to leave and come back. ] ] --- class: small-font # Step 3: test it, tune it, get signed off .pull-left.w48[ Upload and open the **🔌 Serial Monitor**. 1. Distances scroll. Wave your hand: the numbers change. 2. Put **your real target** where it will go: the bowl, a stuffed cat, a hand in the slot, a person at the bin. Write down the distance **with** it and **without** it. 3. Set `NEAR` **between** those two numbers. Upload again. 4. Bring your target in: **"Dispensing!"** and the gate moves, **once**. Leave it there: nothing more. Take it away and bring it back: once again. Press the button: same. .font-small[ **Always 999?** Trig and echo swapped, or the sensor's VCC/GND are off. **Jumps around?** Soft and angled things echo badly; aim at something flat. ] ] .pull-right.w48[ .check[ **✋ Sign-off.** Call Dr. Alvear over and show: - distances scrolling in the Serial Monitor - your two measurements and the `NEAR` you chose - one dispense from the sensor, one from the button Then you're cleared to cut cardboard. ] <br> .emphasis[ **One portion per visit.** Remember the Class 18 flaw, a pile while you held the button? It's gone: the target has to **leave and come back** to get another. That's `wasThere` at work. ] ] --- class: small-font # Step 4: build the prototype .pull-left.w48[ **Cat, cereal, candy: the bottle and gate** 1. Cut the bottom off the bottle. Turn it **upside down**. 2. Build a **stand** that holds it neck-down, high enough for a cup or bowl underneath. 3. **Fix the servo** to the stand, beside the neck. Tape alone wobbles; box it in with cardboard, then tape. 4. Tape a cardboard **flap** to the horn. Closed, it **slides under** the neck and covers it. Open, it swings clear. 5. Mount the **sensor** where it sees the cat, the bowl or the hand, and **not** the falling food. ] .pull-right.w48[ **Trash can: the box and lid** 1. A box with a cardboard **lid**, hinged with tape along one edge. 2. **Fix the servo** inside, so its arm (or a stiff cardboard extension) pushes the lid **up**. 3. Mount the **sensor** on the front, facing whoever walks up. `NEAR` will be **30–50 cm**. <br> .font-small[ **Before you tape the flap or arm on for good:** upload, let the servo go to `CLOSED`, *then* attach. Otherwise your "closed" isn't closed. **Keep the Arduino and breadboard off to the side**, not under the food. Things will spill. ] ] --- class: small-font # Step 5: test it with real stuff .pull-left.w52[ Fill it with something dry and small (rice, beads, Cheerios, kibble). Then run **five trials**, and fill in this table in your notebook: | Trial | What you did | Portions that fell | Jammed? | Notes | |---|---|---|---|---| | 1 | target in, out right away | | | | | 2 | target in, out right away | | | | | 3 | target **stays 5 seconds** | | | | | 4 | target **stays 5 seconds** | | | | | 5 | owner button | | | | .font-small[ Trash can: count **lid openings**. Right now the lid closes even while you're still there; keeping it open is one of next class's rules. ] ] .pull-right.w44[ **Fix the mechanism first, not the code.** - **Food jams in the neck?** Cut the neck wider, or use smaller food. - **Flap catches on the neck?** Lower it. It must slide *under*, not push *up*. - **Too much falls?** Shorter `PORTION`, or a smaller opening. - **Nothing falls?** Bigger `OPEN` angle, longer `PORTION`. - **Servo buzzes and strains?** The flap is rubbing. Trim it. .emphasis[ Trials 3 and 4 should drop **exactly one** portion. More than one? Your target's distance is wobbling across `NEAR`: watch the Serial Monitor and move `NEAR` away from the wobble. ] ] --- class: small-font # Step 6: your machine's rules, in plain English .pull-left.w48[ A rule says **when** the machine acts, and **when it must not**. Write three to five for your version. Short sentences, no code. .font-small[ **Some examples (yours will differ):** - *After the 20th candy, stop and print "Refill me!"* - *Never more than 3 candies in 2 minutes, no matter how many hands.* - *Don't believe a single weird reading. Only act if the hand is there for a moment.* - *The lid stays open while someone is there, and closes 3 seconds after they leave.* - *Count every opening, and print the count.* ] ] .pull-right.w48[ **Then, for each rule, answer:** 1. What does the machine have to **remember** to follow it? *(the last reading? a count? a time?)* 2. How would you **test** it? Write the test: *"Reach in 4 times in one minute. Expect: 3 candies, then nothing."* .emphasis[ Next class you get the **tools** to build these rules, and a program with a spot marked for them. The rules you write today are what you'll build. ] ] --- class: center, middle # Wrap-up — in your notebook .hw-box[ - Your `NEAR`, and the **two distances** you measured to choose it. - A **sketch or photo** of the prototype, with the servo, flap and sensor labelled. - Your **five-trial table**. What jammed, and what did you change? - **Your rules**, in plain English, each with what it must remember and how you'll test it. - **Leave the prototype assembled.** Next class you program it. ]