Challenge 1
Cross a play area and get free after at least three bumps. Nobody may touch the robot. The bump count on the screen must match the number of bumps you saw.
Quick Learn — the model, the instructions, the program and the challenges. The theory is in Full Lesson.
EV3 Robotics›Level 2 · Intermediate›Lesson 39
Level 2 · Lesson 39 · EV3-L02-3960 minutes · Ages 8+ · Model: Auto Floor Mopping
The Auto Floor Mopper. It is a driving base with a mop head. On the front is a bumper joined to a Touch Sensor. It cleans until it hits something. Then it sorts itself out.
Most sensors you have used so far were there to stop something from happening. The saw would not start. The forklift would not lift. The ticket gate would not open without a ticket.
This one lets the crash happen. The bumper is not a warning. It tells you the robot has already hit the wall. What matters is what it does next.
Cleaning machines are everywhere now. People push floor scrubbers round shops and airports. More and more, robots do the same job at night, all on their own.

The robot on the right has costly sensors that look ahead. But it still has a bumper. They all do.
That is because looking ahead does not work every time. Some sensors cannot see glass. A chair leg can be too thin to spot. Something can move in front of the robot too fast. The bumper catches everything the clever sensors missed. The robot is built knowing that they will sometimes miss.
You may have seen a robot vacuum at home do this. It wanders, bumps, backs off, turns a little and carries on. It is not using a map. It gets out of trouble again and again. It cleans the room by never giving up.
Think of a robot that can only try to stop crashes. When a crash happens anyway, it does not know what to do. It might stop and wait for a person. That is no good for a cleaner working at night. Or it might keep driving into the wall until the battery runs out.
Plan only for things going right, and the first surprise stops you. Plan for getting out of trouble, and the robot keeps working.
Getting out of trouble is called recovery. It has three steps, always in the same order. Learn them by heart. You will use them again and again.
| Step | Why |
|---|---|
| 1 · Back away | The robot is touching the thing it hit. Nothing else works until it moves away. A robot that turns while pushed against a wall just scrapes. |
| 2 · Turn | Face a new way. If not, it will drive straight back into the same thing. |
| 3 · Carry on | Go back to normal work. The recovery is done, and the robot forgets about it. |
if <[1 v] is pressed?> then move [backward v] for (1) [rotations v] :: movement move for (0.5) [rotations v] at (35) (-35) % speed :: movement end
See how there is no else? Recovery only happens sometimes. Most times round the loop, there is nothing to fix. So it uses the Lesson 6 shape, not the Lesson 8 shape.
Turn before backing away, and the robot turns while stuck on the wall. One wheel grips. The other one slips. It ends up at an angle, still touching. Back away first, and it has room to turn well.
The bumper must let go. Back up far enough that the sensor is not pressed any more. What if the robot backs up two centimetres and the bumper still touches? Next time round the loop, it sees a new bump. It backs up again, for ever.
Put this robot in a corner and watch. It bumps one wall and backs up. It turns into the other wall. It bumps and backs up again. Then it turns back into the first wall. It can do that all day.
Real machines fix this by counting. Lots of bumps close together? Then do something bigger. Back up further, or turn much more, or give up on that spot. The Battle Rescue in Lesson 18 showed you a variable. A variable can count things that happen.
The Touch Sensor is the simplest input the EV3 has: a button that is either pressed or not. That sounds trivial, but it is how a robot knows it has hit a wall, reached the end of a track, or been told to start by a person.
| Block | What it does |
|---|---|
wait until <[1 v] is pressed? :: sensors> | Holds the program here until somebody presses the sensor. |
<[1 v] is pressed? :: sensors> | Reports true or false. Drop it into a condition to make a decision rather than a wait. |
[1 v] when [bumped v] :: events hat | Starts a whole stack of its own. The dropdown chooses the moment: pressed, released or bumped. |
A button is not only “pressed”. One press is three things: the moment it goes down, the time it stays down, and the moment it comes back up. Watch what a single press does to three programs at once.
versus two hat blocks
The middle counter is the one that surprises people. Nothing is wrong with it — a loop really does check that fast, and every check really is a separate answer.
Nothing there is broken. A loop really does get round hundreds of times a second, and each time it asks is pressed? the honest answer is still yes — so if that loop plays a sound or counts something, it does it hundreds of times from one finger. The two hat blocks each fire once, and they fire at different moments: pressed the instant the button goes down, bumped only when it comes back up.
The three options, and what each is for:
when program starts :: events hat set movement motors to [B v] and [C v] :: movement start moving [straight: 0] :: movement wait until <[1 v] is pressed? :: sensors> stop moving :: movement
The robot drives until something presses the sensor. Note that the movement is started unmeasured on purpose — the sensor decides when to stop, not a distance.
Touch sensors are everywhere in machines you cannot see into: a lift knows the doors are shut, a printer knows the lid is closed, a washing machine will not spin until it is latched. They are safety devices as much as inputs.
Back away first. Always. You cannot steer away from something you are still touching.
Long before there were computers, people had exactly this problem. A shepherd counting sheep through a gate, a trader counting sacks of grain, a builder counting days — none of them can hold the number in their head while they get on with the work. So they scratched a mark on a wall, cut a notch in a stick, or wrote a number on a piece of paper. The number lived outside the person, in a place they had agreed on, and they could go back to it, read it, and change it.
Better still, once the number is written down somebody else can use it. Watch these two: one of them counts and writes, the other never sees a single animal and simply reads the wall.
Notice what never happens: Ben never asks Abby. He does not need to — the number is not in her head, it is on the wall, and the wall is there for anyone who needs it.
Neither Abby nor Ben is holding the number — the wall is. And notice what never happens: Ben does not ask Abby. He does not need to, because the count is not in her head. It is in a place they both agreed on, which is what makes it useful to more than one of them.
That is all a variable is. The robot cannot hold a number in its head either, so you give it a wall of its own, write a name at the top so everyone knows which wall is which — score, count, degree_turn — and the program can read what is on it and write something new. One part of the program writes; another part reads. Exactly Abby and Ben.
Say we are counting rotations of a motor. Before we start we write 0 on the paper. Every time the motor completes a turn we cross out what is there and write one more: 0 becomes 1, then 2, then 3. That is change — it has to read the old number to work out the new one.
set is the other thing you can do, and it is completely different: rub the whole paper out and write the number you want. It does not care what was there. Press the buttons and watch what happens to the crossings-out.
The paper starts blank, so we write 0 on it. That is what a variable is: a place to keep a number while the robot works.
| Block | What it does |
|---|---|
set [count v] to (0) | Puts a value in, replacing whatever was there. |
change [count v] by (1) | Adds to what is already there. |
(count) | Reports the current value, for use in a comparison or on the display. |
set replaces; change adds. Counting things needs change. Starting a count needs set. Both programs below have both blocks — the only difference is whether the set block is inside the loop or above it.
set before the loop
set inside it
Both programs contain both blocks. Only the position of set [count] to 0 is different.
The count on the right is not broken; it is being told to start again on every pass. Each time round the loop it is wiped back to zero and then changed by one, so the honest answer is always 1 — while the motor cheerfully turns four times. A counter stuck at 1 almost always means a set block that has slipped inside the loop.
EV3 Classroom tells you what a block does by its shape, and once you have noticed that, a whole set of questions answers itself:
So when a slot is oval, any oval fits it — and it does not matter in the least where that number came from. You can take the motor’s own A degrees counted and keep it in a variable you named degree_turn, then compare that with a number later. Pick an oval below and watch the same one drop into all three kinds of slot.
pick an oval
the same oval fits all three
Every one of those slots is oval-shaped, and A degrees counted is an oval — so it drops in. Nothing about where the number came from matters.
This is what makes a variable more than a counter. A sensor reading is true only at the instant you read it; copying it into a variable freezes it, so the robot can compare where it is now against where it was when something happened:
when program starts :: events hat [A v] reset degrees counted :: motors set [degree_turn v] to ([A v] degrees counted :: sensors) start moving [right: 30] :: movement wait until <(([A v] degrees counted :: sensors) - (degree_turn)) > (400)> stop moving :: movement
Read the condition aloud: how far the motor has gone now, minus where it was when we started, is more than 400. Both are ovals, so both can go into a subtraction, and the subtraction is an oval too — which is why it can go into a comparison. Ovals nest inside ovals as deep as you need.
A variable keeps its value after the program ends. Run the program again without setting it back and the second run begins where the first left off — the count starts at 14, the robot thinks it has already done the job. Every variable a program changes must be set to its starting value at the top.
A variable is the difference between a machine that repeats a fixed routine and one that responds to how things have gone — counting parts, tracking a score, remembering where it started.
Say this back before moving on: “Back up, turn, carry on.”
| Part | What it is doing here |
|---|---|
| EV3 Intelligent Brick | Watches the bumper while it drives. It counts how many times the robot has been hit. |
| Large Motor ×2 — the drive | Ports B and C. They turn together to drive straight. For the spin, they turn opposite ways on purpose. |
| Medium Motor — the mop | Moves the mop head. It runs the whole time. It uses the start block from Lesson 15, which does not wait. |
| Touch Sensor — the bumper | It sits behind a bumper bar. The bar is wider than the robot. If the bumper is too narrow, the robot’s corners hit things the sensor never feels. |
Sensors go in ports 1, 2, 3, 4. Motors go in ports A, B, C, D. You cannot swap them. If you do, nothing warns you. The robot just will not work.
| Part | Port | Why this one |
|---|---|---|
| Left drive (Large) | B | B is left. The Movement blocks expect this. |
| Right drive (Large) | C | C is right. |
| Mop (Medium) | D | Away from the drive motors. It runs at its own speed. |
| Touch Sensor (bumper) | 1 | Touch is always port 1 in this course. |
Check your own build now:
Two routes, and either is fine. USB is the reliable one and the one to fall back on when a room’s Bluetooth is busy; Bluetooth leaves the robot free to move, which some models need.
Do these in order. Naming the Brick after you go looking for it in the list is how groups end up driving each other’s robots.
EV3 until somebody changes it.EV3.The two failures, every class, every time. The Brick has gone to sleep while you were building — press the centre button to wake it. Or you have paired with the group at the next table, which is why the name matters.
The long version, including Port View and how to read the port tiles, is in the Brick & Bluetooth guide.
Use Bluetooth. This robot drives into things on purpose and backs out again. It would run over a cable within about two bumps.
Use the same space every run. Watch the bump count. It shows how hard your space is for the robot.
<(bumps) > (5)>. Make it do something bigger: back up further and do a half turn. Then set bumps back to 0. Test it in the same corner.Step 5 joins three lessons. The bumper asks a sensor, like the crab in Lesson 37. The choice is the Switch from Lesson 6. The count is the variable from Lesson 18. Only the count lets the robot know it is stuck. None of them could fix it alone.
A recovery that always does the same thing can go on for ever. Counting is how a robot can tell it is not getting anywhere.

Build the model before you read any further. Everything after this is about making it do something, and none of it will make much sense with nothing on the table in front of you.
One page at a time — use Next and Previous, or the ← → keys. Fullscreen makes it big enough to build from.
Check the finished build against the picture before you switch anything on. A motor mounted the wrong way round is far easier to spot now than it is to debug later, when it looks like a program fault.
Mop forward until it hits something. Get out of trouble. Carry on. And count how many times it happened.
when program starts :: events hat
set movement motors to [B v] and [C v] :: movement
set movement speed to (30) % :: movement
set [bumps v] to (0)
clear display :: display
write [MOP] at line (1) :: display
[D v] start motor at (70) % speed :: motors
forever
start moving [straight: 0] :: movement
if <[1 v] is pressed?> then
change [bumps v] by (1)
write (bumps) at line (4) :: display
play beep (50) for (0.2) seconds :: sound
move [backward v] for (1) [rotations v] :: movement
move for (0.5) [rotations v] at (35) (-35) % speed :: movement
end
endWhat success looks like: the mopper crosses the space and hits a wall. It beeps, backs off, turns, and sets off a new way. It does not stop, and nobody touches it. The number on the screen goes up by one for each crash.
Does it back up again and again after one bump? Then it does not back up far enough, and the bumper has not let go. Make it 1.5 rotations. Does the count jump by two or three each hit? Then the bumper is bouncing. The same fix helps.
Work through them in order — each is harder than the last. The extra challenge comes after all three, and it is meant to make you plan before you build.
Cross a play area and get free after at least three bumps. Nobody may touch the robot. The bump count on the screen must match the number of bumps you saw.
Prove that the order matters. Swap the reverse and the turn, so it turns first. Drive it into a wall. Say just what goes wrong. Then put it back.
Get it out of a corner. Drive it into a corner on purpose and watch it fail. Then use the bump count to tell that it is stuck. Make it do something bigger. Test your fix in the same corner.
Clean a whole room, with things in the way. Mark out an area with walls. Put two or three objects inside it. Your machine must cover the floor. It must keep working whatever it hits. Nobody may rescue it. Plan how it gets free on paper first. How far does it back up? How far does it turn? What does it do when it bumps a few times in a row? Decide how long you will let it run. Decide how you will check which parts of the floor it covered. You could use chalk marks, bits of paper, or a sketch of where it went. Then let it run for the full time without touching it. Note every place it got stuck. Answer two questions when you show it. Where did it fail to reach? Was that a problem with getting free, or with covering the floor? Also, your robot turns the same amount after every bump. What would change if it turned a random amount instead? Would that be better?