Challenge 1
Build a crab that never stops walking. It changes speed the moment you touch it. It walks faster while you hold it. It goes back to normal as soon as you let go. Both changes must happen straight away.
Quick Learn — the model, the instructions, the program and the challenges. The theory is in Full Lesson.
EV3 Robotics›Level 2 · Intermediate›Lesson 37
Level 2 · Lesson 37 · EV3-L02-3760 minutes · Ages 8+ · Model: EV3 Dancing Crab
The Dancing Crab. A Large Motor moves its legs sideways. A Touch Sensor is its claw. The Brick can tell when you touch the claw, while the crab is still dancing.
In Lesson 28 the saw waited for the button. It did nothing else until you pressed it. That is right for a saw. But it would make a strange crab. It would stand still until you poked it, and only then move.
This crab dances all the time. Touching it changes the kind of dance. That difference is today’s lesson.
You can see ghost crabs on beaches all over the world. They run sideways, very fast. They stop, wave a claw, and then dive down a hole. They go as soon as your shadow falls on them.

A crab’s legs bend sideways, not forwards. So sideways is the way it is built to run. Its eyes sit up on stalks. It can see almost all the way round.
Put those together and the crab is watching the whole time it moves. It does not stop to check if something is coming. If it did, it would be caught. A crab that stops to look is a crab a seagull can see.
Think of an animal that has to stop to look. While it looks, it cannot run. Animals that stay safe outdoors look while they move. A robot that must keep working while it watches has the same problem.
Waiting for something and watching for something are not the same thing.
The Touch Sensor has a wait block. It also has a hexagon block. The hexagon is a question you can ask at any time. Asking it does not stop the program.
<[1 v] is pressed?> :: sensors
Here are two ways to use the same sensor:
| Block | Shape | What the machine does |
|---|---|---|
| [1 v] wait until [pressed v] | Stack | Stops. Nothing else can happen until the sensor changes. This was the saw in Lesson 28. |
| <[1 v] is pressed?> | Hexagon | Answers and moves on. Put it in a Switch inside a loop. Then the robot checks all the time, while it does other things. |
You have seen this before, on the Radar in Level 1, Lesson 41. The Ultrasonic had a wait until distance block. It also had a distance block that just gives a number. The radar needed the number block. A radar that stopped to wait would never sweep. It is the same idea with a different sensor.
forever
if <[1 v] is pressed?> then
[A v] set speed to (80) % :: motors
else
[A v] set speed to (25) % :: motors
end
endNothing in that loop ever stops. It asks the sensor thousands of times a minute. The crab dances the whole time. The sensor changes the dance. It does not start it.
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.
Wait-until means “wait, then do this.” A hexagon means “keep going, and change how.”
Up to now a robot has been able to wait for a sensor. Deciding is different: the robot checks the sensor and does one thing or another depending on the answer — and then carries on either way.
| Block | What it does |
|---|---|
if <> then end | Runs the blocks inside only when the condition is true. Otherwise skips them. |
if <> then else end | Runs one set of blocks when true and a different set when false. |
A decision made once, at the start, is almost never what you want. Here are two robots with the identical if-else, testing the identical sensor against the identical number — one inside a loop and one not.
decision inside a loop
the same decision, once
A decision is only worth as much as the last time it was made. Inside a loop, that is a few milliseconds ago.
Nothing is wrong with the right-hand program’s decision. It asked the question, got a truthful answer, and acted on it correctly. It simply never asked again, and the world moved on. Decisions belong inside a loop, so the robot keeps re-deciding as things change.
when program starts :: events hat
forever
if <([4 v] distance in [cm v] :: sensors) < (15)> then
stop moving :: movement
else
start moving [straight: 0] :: movement
end
endOnce there is more than one question, decisions can be arranged in four ways. They look nearly identical stacked up in the editor, which is exactly why they get muddled — the thing that differs is not what the blocks say, it is which routes through them exist.
One question sorts them almost completely:
| Are the questions… | Use | How many bodies can run |
|---|---|---|
| independent — any combination can be true | separate ifs | none, some, or all |
| one question, two answers | if / else | exactly one |
| the second only matters when the first is true | nested if | one, and only via the outer |
| mutually exclusive cases — exactly one should win | chained if / else | exactly one, the first that matches |
Each if is asked no matter what the others answered, so any number of them can fire on the same pass. That is the right shape when the conditions genuinely have nothing to do with each other.
forever
if <[3 v] is ambient light intensity [< v] (20) %? :: sensors> then
[A v] start motor [clockwise v] :: motors
end
if <[4 v] is distance [< v] (15) [cm v]? :: sensors> then
[D v] start motor [clockwise v] :: motors
end
endExactly one branch runs, every time. Reach for this whenever the robot must do something either way — and in preference to two ifs testing opposite conditions, which is the same idea written twice and can drift apart.
Putting one if inside another means the inner question is only ever asked when the outer one is true. Use it when the second question is meaningless otherwise: there is no point asking which side an obstacle is on when there is no obstacle.
if <[4 v] is distance [< v] (15) [cm v]? :: sensors> then
if <([2 v] angle :: sensors) < (0)> then
start moving [right: 50] :: movement
end
endWhen not to nest. If you only want “both true” and nothing happens at the outer level, an and says it in one block and reads better:
if <<[4 v] is distance [< v] (15) [cm v]? :: sensors> and <([2 v] angle :: sensors) < (0)>> then start moving [right: 50] :: movement end
Nesting earns its place when something happens at the outer level too, or when there is an else at each level and the two mean different things.
This is the shape for a list of cases where exactly one should win: colour bands, distance bands, speed ranges. EV3 Classroom has no else-if block, so you build a chain by putting the next if inside the else of the last one.
And here is why it matters, because this is the single commonest bug in this whole module. Three bands written as three separate ifs are each perfectly correct, and together they are wrong: a reading of 20 is under 30 and under 60 and under 90, so all three run and the last one to run is the one that sticks.
three separate ifs
chained — if / else / if
↑ the rest is inside the else — never asked
Separate ifs are not wrong here so much as unguarded: nothing stops a second one matching. Chaining is what makes “the first one wins” true.
The rule to carry away: if the cases are meant to be exclusive, they must be made exclusive. Chaining does it by construction. Separate ifs only work if you are careful to write non-overlapping bands yourself — light < 30, 30 to 60, 60 and over — which is more to get right and easy to break later.
This is the point at which a machine stops following a script and starts responding. A thermostat, an automatic door, a robot vacuum — all of them are a decision inside a loop.
Forever means forever. This program never ends by itself. Find the stop button before you run it, just like you did for the saw.
Say this back before moving on: “Asking does not stop anything.”
| Part | What it is doing here |
|---|---|
| EV3 Intelligent Brick | Asks the sensor again and again while the legs keep going. It does two things at once, with just one stack. |
| Large Motor — the legs | Moves the legs sideways. The Switch only changes its speed. That makes the choice easy to see. |
| Touch Sensor — the claw | It sits where a claw would be. So you press it by touching the crab. Here the sensor is part of the animal, not a button panel. |
| The walking linkage (not electronic) | Turns one turn of the motor into a sideways step. Watch which legs are down each time. If the crab drags a leg, a part is one hole out. |
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 |
|---|---|---|
| Leg motor (Large) | A | One motor with one job. It moves all the legs. |
| Touch Sensor (claw) | 1 | Touch is always port 1 in this course. The saw used port 1 too, for its hold-to-run button. Here it is used in a very different way. |
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, and find the stop button first. This program never ends by itself. And the crab walks. It will walk right off the table if you let it.
Keep a hand near the stop button for these. Some of them leave the crab running.
[1 v] wait until [pressed v] followed by a speed change. Run it. The crab stands still until you touch it. Compare it with the program above. Which one is a crab?play beep (75) for (0.05) seconds inside the if. Hold the claw and listen. It beeps again and again. This is the repeat problem from Lesson 6, the Swing Ride. Now you know what it sounds like.Steps 2 and 3 are two halves of one idea. Without the loop, the question is asked only once. Without the else, the answer can only change one way. You need the loop and both sides. Then the robot really keeps up with the world.
A robot that watches must be in a loop. It must also know what to do when the answer is no.

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.
The same build on Google Drive — sometimes a video, sometimes a scan:
Fullscreen it while you build — you can pause and scrub with the player's own controls.
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.
A crab that walks slowly until you touch its claw. It runs fast while you hold it.
when program starts :: events hat
clear display :: display
write [CRAB] at line (1) :: display
[A v] start motor at (25) % speed :: motors
forever
if <[1 v] is pressed?> then
[A v] set speed to (80) % :: motors
write [RUN ] at line (3) :: display
else
[A v] set speed to (25) % :: motors
write [WALK] at line (3) :: display
end
endWhat success looks like: the crab walks as soon as you press Play. Touch the claw and it speeds up straight away. Let go and it slows down straight away. It never stops. You cannot see any delay.
Does the crab stand still until you touch it? Then you have used
wait until pressed, not the hexagon. That is the saw's program. It is wrong for a crab.
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.
Build a crab that never stops walking. It changes speed the moment you touch it. It walks faster while you hold it. It goes back to normal as soon as you let go. Both changes must happen straight away.
Make a touch change the crab's direction, not its speed. The crab walks backwards while you hold it. It goes back to its first direction when you let go. It must never stand still.
Give the crab a startle, like a jump of fright. A touch makes one sound and one flinch you can see. Then it goes back to walking, even if you keep holding the claw. The hard part is making the flinch happen once, not over and over.
Make the crab act like something alive. An animal reacts while it is busy doing something else. It does not react the same way to everything. Your job is to give the crab at least three different behaviours. Each one needs a reason. Choose the behaviours on paper first. Write the rule for each one as a sentence. What does it do? What makes it start? What makes it stop? At least one must start with a touch. The crab must never stand still waiting for anything. Then test it on somebody who has not seen it. Let them play with it for a minute. Do not explain anything. Watch what they try. Answer two questions when you show it. What did your tester expect the crab to do that it did not do? Would adding that make it more lifelike, or just harder to build? Also, your crab only knows one kind of touch. Say what you would add so it could tell a single poke from being held.