Challenge 1
Count ten swings and stop. The machine's count must match the count you made yourself.
Quick Learn — the model, the instructions, the program and the challenges. The theory is in Full Lesson.
EV3 Robotics›Level 2 · Intermediate›Lesson 32
Level 2 · Lesson 32 · EV3-L02-3260 minutes · Ages 8+ · Model: Gymnastic
The Gymnastic is a LEGO gymnast. It hangs by both hands from a high bar. A Medium Motor turns the bar through gears and a rubber band.
Turn the bar and the gymnast swings up, over the top, and round again. That is just like a real gymnast’s giant swing. An Ultrasonic Sensor sits by the frame. It sees the gymnast go past on every turn.
By the end, the Brick will count the swings and stop after ten. Getting that count right is much harder than it sounds.
A gymnast on the high bar does giants. These are full circles round the bar with the body straight. They look easy, but they are not.

A thing that hangs and swings is called a pendulum. A pendulum has its own beat, and you cannot change it. How fast it swings depends on how long it is. It does not depend on how heavy it is. It hardly depends on how far it swings.
So a gymnast cannot swing higher by pulling harder. They get round by pushing at just the right moment. They bend on the way up. They stretch out on the way down. They do it once per swing, in time with the beat. Small pushes at the right moment add up. The same pushes at the wrong moment undo each other. That is called resonance. You do the same thing on a swing in the park.
Your model has the same rule. The motor is not strong enough to throw the gymnast round. It gives a little push through the rubber band each time. That only works if the speed matches the model’s own beat. You saw this first in Level 1’s Clock Ticking. The machine sets the beat, and the program has to match it.
Too fast, and the gymnast shakes instead of swinging. Too slow, and it never gets over the top. There is a right speed, but you cannot work it out on paper. You find it by trying. That is what step 10 is for.
Some machines have a speed they like. Listen to them.
Here is the easy way to count swings. It is wrong. But the way it goes wrong will teach you a lot.
forever
if <([4 v] distance in [cm v] :: sensors) < (15)> then
change [swings v] by (1)
end
endThe Brick runs that loop hundreds of times a second. The gymnast takes about a fifth of a second to go past the sensor. So one swing is not one “close”. It is a hundred of them in a row.
wait until <([4 v] distance in [cm v] :: sensors) < (15)> change [swings v] by (1) wait until <([4 v] distance in [cm v] :: sensors) > (25)>
That third line is the whole lesson. It is called re-arming. The counter is ready. It counts once. Then it cannot count again until the check has gone false.
| What you are asking | What you get |
|---|---|
| Is it close? | A reading. It is true for as long as the gymnast stays close. If you count it, you count time, not swings. |
| Has it just become close? | An event. It is true once each time the gymnast comes. This is what a swing is. |
A button has the same problem. Say each press fires one shot. Hold it down, and it fires every shot. The fix is [1 v] wait until [released v], as on the Saw Machine in Lesson 28. A held button and a passing gymnast are the same problem. Something stays true, and it gets counted again and again.
Use 15 for both, and something bad happens at the edge. Real sensors wobble. The number might jump between 14 and 16. Then it crosses the line many times as the gymnast passes. Each crossing is a new count.
Use two numbers with a gap between them. Now that cannot happen. To count again, the number must go all the way out past 25 first. This is a window: a range of numbers, not one line. Engineers call it hysteresis. Heaters, lifts and street lights all use it, for the same reason.
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.
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.
To count each time it comes, wait for it to leave too. A counter that never re-arms just counts time.
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 moving on: “Arrived, count, left. Then look again.”
Look at your model. Only two parts are electronic. But a third part, made of rubber, works just as hard.
| Part | What it is doing here |
|---|---|
| EV3 Intelligent Brick | The base of the frame, and the scoreboard. Its screen shows the swing count as it goes up. |
| Medium Motor | Turns the bar through gears. It is Medium, not Large. This is a fast, light job. The push goes in a little at a time. |
| Ultrasonic Sensor | Watches the space the gymnast swings through. Here it is a counter, not a ruler. It only has to tell near from far. |
| The rubber band (not electronic) | Joins the motor to the bar, and lets it slip. The slip is a good thing. It stops the motor forcing the gymnast to swing at the wrong beat. |
| The figure and its arms (not electronic) | Must hang loose and go right over the top. A stiff shoulder joint stops the whole trick. |
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 |
|---|---|---|
| Bar drive (Medium Motor) | A | One motor doing its own job. |
| Ultrasonic Sensor | 4 | The Ultrasonic always goes here. Both checks use this port. |
The lesson list says you may add a Touch Sensor. The build guide does not have one. It is an idea, not part of the build. Want a start button? Fix one on port 1 yourself. The challenges will ask for it.
Check your own wiring 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 out of the swing circle. The gymnast goes right round the bar. A cable in that circle will be counted as a swing. Or it will stop the swing.
Do not skip step 3. The 15 and 25 in the program below are guesses about your model. The tile has the real answer. Stuck? Read the Brick & Bluetooth guide.
One change at a time, and guess first. The first two are about the counter. The rest are about the machine’s beat.
wait until … > (25). Guess what the count will do. Then run it for two seconds. This is the bug this lesson stops. It is worth seeing once.Step 6 uses the maths from Lesson 22, the Speed Bot, in a new way. The machine counted the swings. It also timed them. So it can work out its own period. You did not add a sensor for that. You divided two things it already knew.
A counter counts each time a line is crossed. Make sure each crossing means what you think.
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.

Turn the bar. Count ten real swings. Then stop and clap.
when program starts :: events hat set [swings v] to (0) clear display :: display write (swings) at line (1) :: display [A v] start motor at (55) % speed :: motors repeat until <(swings) = (10)> wait until <([4 v] distance in [cm v] :: sensors) < (15)> change [swings v] by (1) write (swings) at line (1) :: display wait until <([4 v] distance in [cm v] :: sensors) > (25)> end [A v] stop motor :: motors play sound [Communication / Hello v] until done :: sound
start motor, not run for. The bar must keep turning while the loop counts. You learned this in Lesson 15 and used it on the Saw Machine, in Lesson 28.What success looks like: the gymnast swings up and over, round and round. The number on the screen goes up by one each swing. You can count along. After the tenth, the bar stops and the Brick says hello.
Does the count jump to 10 straight away? The sensor can see something close all the time. Or your re-arm number is too high to reach. Does it never count? The gymnast is not getting close enough. Check tile 4 by hand again. Does the gymnast shake instead of swinging? That is not the program. Change the speed.
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.
Count ten swings and stop. The machine's count must match the count you made yourself.
Find the motor speed that gets the figure over the top most often. Do three runs at every speed you try. Write the results down.
Make the machine measure its own swing period in seconds. Show it on the screen. The period is how long one full swing takes. Use the timer and the swing count together.
Make the machine find its own best speed, with nobody touching it. It must try three speeds, one after another. For each speed, it counts how many full swings happen in fifteen seconds. It remembers which speed was best. Then it settles down and runs at that speed.