Challenge 1
Run five laps and count them. If a lap fails, stop at once. Be honest and say so.
Quick Learn — the model, the instructions, the program and the challenges. The theory is in Full Lesson.
EV3 Robotics›Level 2 · Intermediate›Lesson 46
Level 2 · Lesson 46 · EV3-L02-4660 minutes · Ages 8+ · Model: Ball Coaster
The Ball Coaster: a track with a loop in it. A ball rolls down, round the loop and along to the end. Then a Large Motor lifts it back up to the top, so it can go again.
An Ultrasonic Sensor watches the end of the track. When the ball gets there, the machine knows the run is over. Then it can start the next one.
By the end of the lesson, the coaster will run laps on its own. When the ball gets stuck, it will stop in a sensible way. On a machine like this, the ball will get stuck. That is certain!
A roller coaster with a loop pulls its train up a big first hill. Then it lets go. After that, gravity does everything. There is no engine, and no brakes until the end. The height of that first climb pays for the whole ride.

Up on the hill, the train has stored energy. That is called potential energy. Coming down, it turns into speed. Energy of moving is called kinetic energy. The loop uses some of it to climb again. Some energy is lost on the way, because the wheels rub on the track. That rubbing is called friction. So the train can never climb back as high as it started. That is why the loop is always lower than the first hill.
Your ball follows the same rule. If the loop is too tall, the ball crawls round the top and falls off. Look closely at real loops. They are not circles! They are squeezed in at the top. So the train turns tightest where it is going slowest.
Now for the part that matters today. Real coasters split the track into sections. The computer knows how long a train should take to get through each one. What if a train is too slow? The ride does not just wait. It stops every other train on the track and calls an engineer.
It has to. If the computer just waited for the sensor, it would wait for ever. A train would be stuck on the track, and the next one would still be coming.
Waiting for something that never comes is the same as being broken. It is just quieter.
Here is the program you would probably write first.
[4 v] wait until distance [< v] (10) [cm v] :: sensors
Then the Brick waits. The screen does not change. No error shows up. Nothing beeps. A program stuck on a sensor looks just like a program that is thinking. That is why it is such a nasty bug. There is nothing to see.
reset timer wait until <<([4 v] distance in [cm v] :: sensors) < (10)> or <(timer) > (8)>>
A time limit on a wait is called a timeout. The time limit itself is the deadline. This uses the or from the Jazz Drum in Lesson 30, in a new way. Here, the job races the clock.
or ended the wait. It did not say which side ended it. The program only knows that something happened. To do the right thing, it has to ask a second question. The Hand Speed game in the next lesson does the same thing, to find out who pressed.
if <([4 v] distance in [cm v] :: sensors) < (10)> then change [laps v] by (1) else write [BALL STUCK] at line (3) :: display stop [and exit program v] end
The two endings must lead to different actions. What if the timeout just carries on as normal? Then it does no good at all. The machine lifts a ball that is not there. It does that for ever. And still nobody knows.
A good timeout says what went wrong. Yours writes it on the screen. Then anyone walking past can see what happened.
| Deadline | What you get |
|---|---|
| Shorter than a good run | False alarms. A slow lap that worked is reported as stuck |
| A little longer than the slowest good run | Right. Time your own laps and add a little extra |
| Much longer | Works, but the machine sits stuck for a long time first |
So you measure a deadline. You do not guess it. You did the same with the overshoot on the Tank Bot in Level 1, Lesson 48. You did it with the ratio in Lesson 33 too.
A wait pauses for a length of time. The timer is different: it runs in the background and can be read at any moment, so the robot can know how long something has taken while it is still happening.
| Block | What it does |
|---|---|
(timer) | Reports the seconds since the timer was last reset. |
reset timer | Sets it back to zero, so the next reading counts from here. |
The most valuable use of a timer is escaping a wait that might never end. A robot told to drive until it sees a wall will drive for ever if the wall is not there. Combined with a timer, it can give up:
Repeat until the wall is close or five seconds have passed. That one change turns a program that can hang into one that always finishes. Both robots below are looking for a wall that is not there.
no way out
with a timeout
The sensor is not faulty and the program is not wrong. There is simply no wall, and only one of these two programs has a way of noticing that.
The left-hand robot is not broken, and neither is its sensor. Its condition is simply one that will never come true, so the program sits on that block for ever — with nothing on the Brick to say so. The right-hand program asks the same question with an escape route bolted on, and finishes every time.
Real systems time themselves out constantly — a lift that cannot close its doors eventually gives up and beeps rather than trying for ever. A robot with no timeout simply stops responding, and there is nothing on screen to say why.
The Ultrasonic Sensor measures distance. It sends out a burst of sound too high for people to hear, listens for the echo, and works out how far away the surface is from how long the echo took — exactly how a bat finds a moth, and how a submarine uses sonar.
| Block | What it does |
|---|---|
([4 v] distance in [cm v] :: sensors) | Reports how far away the nearest thing in front of the sensor is, as a number in centimetres. |
wait until <([4 v] distance in [cm v] :: sensors) < (15)> | Holds the program until something comes closer than 15 cm. |
This is the important step up from the Touch Sensor. Touch gives you true or false; the Ultrasonic gives you a number, and the deciding is left to you. Pick a threshold below and watch where the robot ends up.
The black line on the bar is the threshold; the blue fill is the reading. The robot stops the instant the fill crosses the line.
Three different robots, and only one number is different between them. That is what having a number rather than a yes-or-no buys you: the behaviour is tuned by editing one slot, not by rebuilding the program. It also means the sensor can never tell you it is “close” — close is a decision you make about a reading.
Car parking sensors, automatic doors at a shopping centre, and the sensor that stops a lift door closing on somebody all work this way. Reacting before contact is what makes a machine feel safe.
The Home/Retail EV3 set (31313) ships an Infrared Sensor and a Beacon in place of the Ultrasonic and Gyro sensors. The Infrared Sensor also measures distance, so the programs in this module work with it — but it reports a rough 0–100 proximity rather than real centimetres, and it is affected by sunlight and by dark surfaces in ways the Ultrasonic is not.
Every wait needs a deadline. Every deadline needs its own thing to do when time runs out.
A sensor that reports a number cannot be used to make a decision on its own — 23 is neither true nor false. An operator turns that number into an answer by comparing it with something.
| Block | What it does |
|---|---|
<(x) > (50)> | True when the left value is bigger than the right. |
<(x) < (50)> | True when it is smaller. |
<<> and <>> | True only when both conditions are true. |
<<> or <>> | True when at least one of them is. |
Forget the symbols for a moment. A comparison is a question about position on a number line: is x to the left of the other number, or to the right? Left is smaller, right is bigger — and that is the whole of it.
Drag the orange x and the black marker, and change the comparison. The green stretch is every position of x that would make the answer true — so you can see where the answer flips before you get there. Turn not on and watch the green jump to the other side.
Drag either marker, or use the arrow keys.
is x to the LEFT of it?
The lab above asks one question at a time: is this x true? A robot never has just one x, though — a sensor reading slides up and down all the time, so what really matters is which stretch of the line makes the condition true. This one draws the whole answer at once.
Drag the circle to move the number you are comparing against, and change the comparison. Everything shaded green is a value of x that would make it true.
Drag the circle, or use the arrow keys. It moves in steps of 0.2.
Every number to the left of 0.2 — but not 0.2 itself, so the circle is hollow.
Watch the circle, because it carries the part everyone gets wrong:
Now turn not on with x > 2 selected and watch two things happen together. The shading jumps to the other side, and the circle fills in — because “not greater than 2” means 2 or less, and 2 has to be part of it. That pairing is the whole reason a hollow circle is worth drawing.
Why a robot cares. Two conditions that look almost identical — light < 30 and not (light > 30) — differ by exactly one value, the reading of precisely 30. A robot sitting right on its threshold behaves differently under the two, and that is the sort of bug that only shows up occasionally and looks like a broken sensor.
These three join answers together rather than numbers. The trap is that English is looser than a program: “stop if it is close and the bumper is pressed” sounds like it covers both situations, when it covers neither on its own.
Flip the two conditions and watch the table. There are only four possible situations in total, and and and or differ on exactly two of them.
| close | bumper | and | or |
|---|---|---|---|
| true | true | true | true |
| true | false | false | true |
| false | true | false | true |
| false | false | false | false |
and is fussy: it wants both. Three of the four rows are false.
Two sensors are running below: an Ultrasonic reporting a number, and a Touch Sensor reporting true or false. Watch the comparison turn the number into an answer, and watch and and or disagree.
and was true in one row out of four. or was true in three. That is the whole difference, and it is why one of them makes a robot look broken.
The comparison is doing one job: it takes a reading that is neither true nor false and, by holding it against a number you chose, produces something a decision can use. The moment the blue fill crosses the black marker is the moment the answer changes.
The number you compare against is a design decision, not a fact. “Close” for a parking sensor might be 15 cm; for a robot arm it might be 3. Pick it by measuring what the sensor actually reads in the situation you care about, then leave a margin.
and narrows: both must hold, so the robot acts less often but more certainly — stop only if something is close and the bumper is pressed. or widens: either will do, so the robot acts more readily — stop if something is close or the bumper is pressed.
In the four situations above, and was true in one of them and or in three. That is the practical difference: swapping one for the other does not adjust a robot slightly, it changes how often it reacts at all.
Say this back before you move on: “It arrived, or the time ran out. Find out which.”
Look at your coaster. Only two parts are electronic. So where is all the work being done?
| Part | What it is doing here |
|---|---|
| EV3 Intelligent Brick | Runs the machine and tells you how it is going. Today its screen shows the lap count. When things go wrong, it shows why. |
| Large Motor — the lift | Lifts the ball and tips it back onto the start. It is a Large Motor, because lifting things up is just what Large Motors are for. |
| Ultrasonic Sensor — the end checker | Points along the end of the track. It answers one question: is the ball here yet? |
| The loop (not electronic) | This is where runs fail. It must be well below the top of the drop. If not, the ball will be too slow at the top of the loop. |
| The track and joints (not electronic) | The ball can catch on any joint. Check here when the ball starts getting stuck. |
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 |
|---|---|---|
| Lift (Large Motor) | A | One motor doing its own job. |
| End checker (Ultrasonic) | 4 | The Ultrasonic Sensor’s usual home. Both halves of the timeout use this port. |
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.
Keep the cable away from the loop. The loop is the tightest part of the track. The ball has the least energy to spare there. A cable near it will cause the very jam you are trying to catch.
Stuck? The long version, with a photograph of every screen, is in the Brick & Bluetooth guide.
Change one thing at a time. For some of these, you will need to jam the ball on purpose. A finger on the track is the easiest way.
or (timer) out, so only the distance is left. Now hold the ball. The Brick waits, silently, for as long as you like. That silence is what a stuck program looks like.else branch. Let it carry on as normal. Hold the ball and watch. The machine lifts nothing, counts nothing, and keeps going. A timeout that does nothing is just for show.stop [and exit program]. Put in a fix instead. Run the lift backwards half a rotation, then forwards again, and try the same lap once more. Some jams shake loose. It is the bump-and-recover from Lesson 39, in a new place.change [failed] by (1), and only stop when failed reaches 3. A machine that gives up after one bad lap is annoying. A machine that never gives up is broken.Steps 5 and 6 together are the best answer. Noticing a problem, fixing it, and giving up after a few tries are three different jobs. A machine that does all three can be left to run on its own. That is the whole reason people build machines to do jobs.
A program that cannot fail has not been tested. A program that cannot notice it failed cannot be trusted.
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.

Five laps, counted. If the ball ever does not arrive, the robot stops and tells you.
when program starts :: events hat
set [laps v] to (0)
clear display :: display
repeat (5)
[A v] run [clockwise v] for (1) [rotations v] at (50) % speed :: motors
reset timer
wait until <<([4 v] distance in [cm v] :: sensors) < (10)> or <(timer) > (8)>>
if <([4 v] distance in [cm v] :: sensors) < (10)> then
change [laps v] by (1)
write (laps) at line (1) :: display
else
write [STUCK ] at line (3) :: display
play beep (45) for (1) seconds :: sound
stop [and exit program v]
end
end
play sound [Communication / Goodbye v] until done :: soundreset timer just before the wait. The timer must time this run, not the whole program. Put it outside the loop, and every lap after the first runs out of time.or. They have nothing in common, except that either one ends the wait.if reads the sensor again. That is the only way to find out which side won. It is why the sensor is used twice.What success looks like: the lift raises the ball. The ball runs the loop and reaches the end. The screen counts 1, and it goes again. Five laps, then goodbye. Then catch the ball halfway, on purpose. Eight seconds later, the Brick says STUCK and stops.
Does it say STUCK on a lap that clearly worked? Your deadline is shorter than a slow run. Look back at your ten hand-timed laps. Does it count a lap when the ball did not arrive? The sensor is seeing the track.
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.
Run five laps and count them. If a lap fails, stop at once. Be honest and say so.
Add a way to recover. A timeout is when the wait takes too long, so the machine stops waiting. On a timeout, shake the lift. Then try the same lap again, once. If it fails again, give up.
Count failures and laps as two different numbers. Only give up after three failures. Keep both numbers on the screen.
Run the coaster for five minutes with nobody looking after it. At the end, it shows a report. It shows the laps done and the failures. It shows how many times it got going again, and the slowest lap time. Every number must be one the machine measured. None can be one you guessed.