Challenge 1
Ten hits, evenly spaced, then stop. Someone across the room should be able to count them without looking. The machine must stop by itself with the hammer down. No forever loops.
Quick Learn — the model, the instructions, the program and the challenges. The theory is in Full Lesson.
EV3 Robotics›Level 2 · Intermediate›Lesson 7
Level 2 · Lesson 7 · EV3-L02-0760 minutes · Ages 8+ · Model: Hammer Bot
The Hammer Bot. A motor turns a lumpy wheel called a cam. The cam lifts a hammer. Then the hammer falls. It does this again and again, as fast or slow as you choose.
Look at what the motor has to do. A motor can only spin. But the hammer needs to go up and down. How? One odd-shaped piece sits between them. That piece turns the spin into up and down.
Today you will split one spin into two halves. One half lifts the hammer. The other half lets it go.
Every petrol car engine has cams inside. Lots of them. They sit on a bar called the camshaft. The bar has lumps along it, and it spins. Each lump pushes a small door in the engine open at just the right time. Then it lets the door snap shut. That small door is called a valve.

An engine has one main part that spins. It is called the crankshaft. But a dozen parts need to go up and down, in the right order. One camshaft moves them all. Each lump points a different way around the bar. It is set at a different angle. So each valve opens at its own time.
That is why people talk about engines in degrees. A mechanic does not say a valve opens “a bit before the top”. They say it opens 12 degrees before the top. The shape of the lump decides how far the valve lifts. Its angle on the bar decides when.
Set a camshaft a few degrees wrong, and the engine runs badly. Or it breaks itself. A valve that is still open gets hit by the part coming up. A whole engine can be ruined by an angle that is out by less than one gear tooth.
A cam turns “going round” into “going up and down”. The angle decides when.
The cam does not do the same thing all the way round. For about half a turn, it lifts the hammer. For the other half, the hammer comes back down.
| Where the cam is | What the hammer is doing | What you want the motor to do |
|---|---|---|
| 0° → 180° | Going up. The cam lifts its weight. | Go slowly. This is the hard half. If you rush it, the model jumps. |
| 180° → 360° | Falling down by itself. | Get out of the way fast. The faster the cam moves away, the harder the hammer lands. |
So there are two halves, at two speeds. To write that, you must say “half a turn”. We measure part of a turn in degrees.
[A v] run for (180) [degrees v] at (30) % speed :: motors [A v] run for (180) [degrees v] at (100) % speed :: motors
| Degrees | Part of a turn | Looks like |
|---|---|---|
| 90 | a quarter | A square corner. Pointing up, then pointing right. |
| 180 | a half | Straight up to straight down. Today’s number. |
| 270 | three quarters | All the way round, less one quarter. |
| 360 | the whole thing | One whole turn. The same as 1 rotation. |
You could write 0.5 rotations instead of 180 degrees. The motor would do the same thing. But use degrees anyway. People who build machines talk in angles. So do the cam maker and the mechanic. So will every lesson from here to Level 4.
Everything so far has been relative: move 90 degrees from wherever you are now. Sometimes what you actually want is absolute: go to the 90-degree position, whatever position you happen to be in at the moment.
Both pointers below do the same job over and over: swing out a quarter turn, then come home. The only difference is the block that brings them back. Watch one trip and they look identical — so let it run for several.
relative — by an amount
absolute — to a place
Let it loop. Nothing about the left program is wrong — it is just that every trip starts from wherever the last one finished, so a small error is never corrected, only added to.
A pointer moved relatively slowly wanders: each movement starts from wherever the last one finished, so any small error is carried forward and added to. Each orange dash is a trip that called a slightly different place “home”. Nothing about that program is wrong — it is simply that a relative move has no idea where it is supposed to end up.
An absolute movement has no memory of the error. Told to go to position 0, it goes to position 0 — the same place every time, however wrong it was beforehand. That makes it the right tool for a home position: the known place a mechanism returns to before it starts work.
This is possible because an EV3 motor counts its own turning all the time. Its position can be read back, and can be reset to zero — which is how you tell the motor that here is what you mean by zero, usually at the start of a program with the mechanism held at its resting place.
A clock hand returns to twelve; a car’s steering returns to centre; a 3D printer homes its head before every print. All of them go to a position rather than moving by an amount.
Use rotations for how far it goes. Use degrees for where it stops.
Machines repeat. A wiper sweeps, a conveyor runs, a ride goes round — and none of that should mean copying the same blocks over and over. A loop says “do this again” once.
| Block | What it does |
|---|---|
repeat (10) end | Runs the blocks inside a set number of times, then carries on below. |
forever end | Runs the blocks inside over and over, and never carries on below. |
repeat until <> end | Repeats until a condition becomes true — a loop with a sensor as its exit. |
Anything placed after a forever loop will never run. Not “runs late” — never. Both programs below end with the same block: set the status light green.
repeat (3)
forever
↑ this block never runs
Both programs contain the same green-light block. Let it run as long as you like — the right-hand ring will never turn green.
The repeat loop counts its three passes, stops, and moves on to the block underneath, so its light turns green. The forever loop reaches the bottom of its own blocks and jumps straight back to the top, so the block underneath is never reached — however long you leave it. If a program seems to stop half way through, look for a forever loop above the blocks that are not happening.
A program is a list, and the Brick works down it once. Every block runs, in order, and when the last one is done the program is over. That is fine for a list of instructions — drive, turn, beep, stop — because each is a thing you do once.
A sensor is not a thing you do once. Asking is 1 pressed? gives you an answer about this instant, and an instant later it may be wrong. Checking a sensor once tells you what the world was like at the moment the program started — which is almost never what you wanted to know.
So a program that has to react must ask again, and again, for as long as it is running. That is the whole job of the loop: not to repeat an action, but to keep the question being asked.
Wrap a sensor check and the motor it controls in a forever loop and you have built a closed-loop control system — the pattern behind every line follower, thermostat and cruise control:
It is called closed because the output feeds back round to the input: the motors move the robot, moving the robot changes what the sensor sees, and what the sensor sees changes the motors. Break the circle at any point and the robot stops responding.
when program starts :: events hat
forever
if <[1 v] is pressed? :: sensors> then
[A v] start motor [clockwise v] :: motors
else
[A v] stop motor :: motors
end
endRead it as a sentence and it is almost too simple to need explaining: for ever, if the button is pressed run the motor, otherwise stop it. The motor now follows the button for as long as the program is running.
This is the mistake nearly everybody makes first, and it is a hard one to spot because nothing about it looks wrong:
when program starts :: events hat if <[1 v] is pressed? :: sensors> then [A v] start motor [clockwise v] :: motors else [A v] stop motor :: motors end
The logic is perfect. The ports are right. Nothing is misspelled. And the robot will ignore the button completely — because the Brick reaches that if/else a few milliseconds after you press Run, finds the button not pressed, takes the else branch, stops the motor, runs out of blocks and ends. By the time a finger arrives, there is no program left to notice it.
with forever — a closed loop
without it — the common mistake
↑ running — for the only time
Both programs contain exactly the same if/else. The only difference is the forever block around one of them.
Both programs contain exactly the same if/else. The counter is what gives it away: one keeps checking for as long as it runs, the other is stuck on the single check it made before anybody touched anything. A student who has seen this once stops writing it.
The tell on a real robot is a program that ends the instant you start it — the Brick returns to its menu almost immediately. If a sensor program finishes rather than waits, the loop is what is missing.
Say this back before you move on: “360 degrees is one whole turn.”
| Part | What it is doing here |
|---|---|
| EV3 Intelligent Brick | Counts the cam’s angle. It is also the heaviest part. Its weight stops the bot hopping across the desk when the hammer lands. |
| Large Motor — the cam | Turns the cam. It only ever spins one way. All the up and down comes from the cam’s shape. The motor never changes direction. |
| The cam (not electronic) | The wheel or peg that is not in the middle. Its shape decides how high the hammer lifts. The motor only decides how fast. |
| The hammer arm (not electronic) | Falls by its own weight. Nothing pushes it down. That is why a heavier head hits harder. |
Before you write anything, turn the cam slowly with your fingers. Watch the hammer.
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 |
|---|---|---|
| Cam motor (Large) | A | A job motor. It needs a strong turning push to lift the hammer. That push is called torque. That is why it is the Large one. |
| Sensors | none | The Hammer Bot cannot tell where its hammer is. It only counts. It does not sense. So the counting has to be right. |
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.
USB is fine. The Hammer Bot stays in one place. Just keep the cable away from the hammer. If you don’t, it will get hit ten times a run.
Make one change at a time. For this one, listen more than you watch. The beat and how loud it is tell you the most.
Step 3 tricks nearly everyone. Two blocks of 90 degrees make half a turn. So the cam only goes all the way round once every two loops. Ten loops give you five hits, not ten. The loop count did not change. What one loop does changed.
A loop repeats what you put in it, not what you meant to put in it.

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.
Ten hits. Each one is a slow lift and a fast drop. There is a short pause between them, so you can hear the beat.
when program starts :: events hat repeat (10) [A v] run for (180) [degrees v] at (30) % speed :: motors [A v] run for (180) [degrees v] at (100) % speed :: motors wait (0.5) seconds end
What success looks like: ten clear hits, with the same gap between each. They all sound about the same. Then it stops by itself with the hammer down.
Do the hits get weaker as it goes? Then the cam is slipping out of place. Each turn ends a few degrees off. Over ten hits, that adds up. Try a slower lift. A motor that stops from full speed goes too far. A slow motor stops closer to the right place.
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.
Ten hits, evenly spaced, then stop. Someone across the room should be able to count them without looking. The machine must stop by itself with the hammer down. No forever loops.
Give it a rhythm. Make one hit in every three land harder than the other two. That is a strong beat and two soft ones, over and over. You must be able to hear the difference. The only thing you may change is how the cam moves.
Park it safely. After its last hit, the hammer must end up RAISED and stay up. Then nothing is resting under it when the program ends. To do this you need the right number of degrees and the right stop setting. Say which two blocks you used, and why.
Build a machine that tests how hard things are. A hammer that always hits the same way can measure things. Use it to put three materials in order, from softest to hardest. Try a lump of plasticine, a folded piece of paper and an eraser. Or use anything else your teacher says is OK. Plan the test before you run it. A fair test means every material gets the same hit. It comes from the same height, the same number of times, in the same place. Decide what you are measuring. It could be how deep the dent is. It could be if it springs back, or how it sounds. Write it down first. If you decide afterwards, you may trick yourself into the answer you expected. Then run it. Write down what happened for each material. When you show it, answer two questions. What did you have to keep the same for the test to be fair? Which of those was hardest to keep the same? And a real hardness tester drops a weight. It does not push it with a motor. What does your cam do to the hit that a falling weight would not?