Skip to mission
Brain Bots
Mission 22
Step 1/6
Report a problem or missing part
Brain Bots
30 minmedium

MISSION 22 · INTERMEDIATE ARDUINO

Object Passage Counter

Jump to the build step

Make object passage counter. Explore entry edge, rearm and reset.

Public beta missionThese revised instructions are open for learning while actual-kit testing is pending. A grown-up must confirm every setup check before power is connected. If a rating, pin card or required check is missing, explore the idea on screen and leave the physical build unpowered.

3 · Echo Expedition

Your new skill: Entry edge, rearm and reset

Revisit the skill you will reuse: hit the distance zone

WHAT YOU'LL USE

Match these to your kit

Arduino UnoUSB A-to-B data cableHalf-size breadboardMale–male / male–female jumper wires (as needed for actual module terminals)HC-SR04, 1 button

Gather from home

Computer with Arduino IDE and Serial MonitorFlat cardboard targetHousehold ruler

Reuse the parts between missions; these are not 45 simultaneous installations. Red/yellow/green means those three monochrome colours. RGB is a separate component with three channels.

BEFORE YOU BUILD

Unplug USB before changing connections. Use only the USB supply for this build; never connect 9V to 5V or a signal pin, and never connect to mains. An adult verifies variant, printed pin labels and resistor values before powering. Stop and unplug if a part becomes hot or damaged.

YOUR REWARD

+30 XPMission completion

PREDICT BEFORE YOU BEGIN

Before the test: which input will change, and what signal or reading do you expect?

BEFORE YOU BUILD

Parent check: exact part variant, labelled pins and stated values. If a check cannot be confirmed, keep this physical build unpowered and explore the code and prediction instead.

YOUR CHALLENGE

Build object passage counter and prove its rule.

NEW POWER-UP

Entry edge, rearm and reset

You have explored bounded interval. Now try entry edge, rearm and reset.

Adult help: Adult helps with computer setup, first wiring and any water/cutting. Learner predicts, tests and explains.

See the idea before you build

How a breadboard connects insideBreadboard connection groupsWithin one numbered row A to E are connected, and F to J are connected separately. The centre gap separates them. Adjacent numbered rows are separate. Rail breaks vary; check your supplied board.151617ABCDEFGHIJCentre gap: no internal connection across

Each coloured strip is one connected group. Different numbered rows stay separate. This explains the board; use the mission’s exact connection reference for component positions. Check rail breaks on your supplied board.

Open the complete Arduino code

Copy the complete code into Arduino IDE or download the sketch. Follow the matching component card before connecting power.

// First Wonder Brain Bots 22 — Object Passage Counter
// USB-powered model. Unplug before rewiring. Serial Monitor: 9600 baud.
int countValue=0;
bool wasActive=false;
const byte buttonPins[]={2};const byte buttonCount=1;
bool rawButton[4]={HIGH,HIGH,HIGH,HIGH},stableButton[4]={HIGH,HIGH,HIGH,HIGH},pressedEvent[4]={false,false,false,false};
unsigned long changedAt[4]={0,0,0,0};
void scanButtons(unsigned long now){for(byte i=0;i<buttonCount;i++){
 pressedEvent[i]=false;bool raw=digitalRead(buttonPins[i]);
 if(raw!=rawButton[i]){rawButton[i]=raw;changedAt[i]=now;}
 if(now-changedAt[i]>=30&&raw!=stableButton[i]){stableButton[i]=raw;pressedEvent[i]=raw==LOW;}
}}
bool press(byte index){return pressedEvent[index];}
float distance=-1,previousDistance=-1,minimumDistance=9999;
unsigned long lastDistance=0,previousTime=0;
bool sampleDistance(unsigned long now){
 if(now-lastDistance<100)return false;lastDistance=now;
 digitalWrite(9,LOW);delayMicroseconds(2);digitalWrite(9,HIGH);delayMicroseconds(10);digitalWrite(9,LOW);
 unsigned long us=pulseIn(10,HIGH,25000);distance=us==0?-1:us*0.0343/2.0;return true;
}

void setup(){Serial.begin(9600);pinMode(2,INPUT_PULLUP);pinMode(9,OUTPUT);pinMode(10,INPUT);Serial.println("Ready. Follow your mission test.");}
void loop(){
  unsigned long now=millis();
  scanButtons(now);
if(press(0)){countValue=0;Serial.println("RESET 0");}
if(sampleDistance(now) && distance>=0){
 if(!wasActive&&distance<15){countValue++;wasActive=true;Serial.println(countValue);}
 if(wasActive&&distance>25)wasActive=false;
}
}
Check the connection map

CONNECTION MAP

Build the electrical path before you test.

Keep power disconnected while changing connections.
JoinConnectWhy it matters
PowerSupplied USB data cable → Uno USB socket; computer at other endThe same cable uploads and supplies this model. Unplug before wiring.
Shared referenceUno GND → breadboard ground railAll module GND terminals use this same reference. Check whether your rail is split.
Button 1D2 → one switched terminal; opposite switched terminal → GNDINPUT_PULLUP: press reads LOW. Four-leg buttons have internally joined pairs; use a verified switched pair.
HC-SR04 supplyLabelled VCC → 5V; GND → GND5V Uno only; verify printed labels.
HC-SR04 signalTRIG → D9; ECHO → D10Keep sensor dry, stable and facing a flat card.
Kit variant and values confirmed by an adult
All pins match the map; USB was disconnected while wiring
Test includes expected, non-triggering and recovery cases

BUILD PATH

STEP 1 OF 6

Meet your mission

Try · Notice · ExplainRead the checkpoint, make a prediction, then test your idea.

You will explore entry edge, rearm and reset. Gather the kit and home materials below. Unplug USB. Match the part names and printed labels.

Watch this step

A video demonstration will appear here when it is ready. You can follow the written step now.

Read the video instructions

You will explore entry edge, rearm and reset. Gather the kit and home materials below. Unplug USB. Match the part names and printed labels.

Need a diagram for this step?

WHAT'S REALLY HAPPENING?

Why it works

Entry edge, rearm and reset. Ultrasonic measures a round-trip echo; no return is unknown. Check your rule against an example where it should respond and one where it should not. Rearming prevents one parked target counting repeatedly.

This is a tabletop learning model. Real systems also need reliable power, robust sensors and careful handling of unknown measurements; this kit model is not a safety or security device.

FAIR-TEST CHALLENGE

Change one input or setting at a time; keep the wiring and protection unchanged. Repeat three trials.

WHEN IT DOESN'T WORK YET

Debug one clue at a time.

Upload or port missing

Likely cause: Wrong board, port or cable connection

Try this first: Check Arduino Uno selection and supplied data cable. Unplug external wiring before checking upload on built-in LED.

Signal does not match the test

Likely cause: Pin labels, input calibration or variant differs

Try this first: Unplug. Compare every connection with the map. Return to the earlier single-input mission; test one subsystem at a time.

Reading is unknown or unstable

Likely cause: Sensor settling, target, contact or calibration

Try this first: Follow the exact test setup; allow settling, use a flat target or check module pinout unpowered. Never replace unknown with a guessed safe reading.

CLEAN, RESET AND STORE

Disconnect USB, remove wires carefully and store components dry.

NEXT SKILL

Difference over actual elapsed time