The EV3 Machine Gun: a motor and a rubber band flick LEGO bricks out of a holder, one after another. Two Touch Sensors control it. One fires a single shot. The other keeps firing while you hold it down.
Every model so far has had one sensor at most. So the ports rule was easy to follow. But it was hard to see why it matters. With one Touch Sensor, there is only one place it can go.
Today you have two of them. They are the same part, the same shape and the same colour. They look exactly alike. The only way the robot can tell them apart is which port the cable is in.
In the real world 5 min
Where you have seen it
Your model stores energy, lets it out to fire one thing, loads again, and repeats. That is not really a weapon. It is how a nail gun works. You will find one on almost every building site.
An air-powered nail gun, with its long row of nails. Photo: Jonas Bergsten / Wikimedia Commons (Public domain).
Why it is built that way
A nail gun has a row of nails and a store of energy. The energy is squashed air or a spring. Each time, it lets out just enough energy for one nail. Then a spring pushes the next nail into place. It is ready again.
Here is the part that matters today. A real nail gun has two controls, and they are different on purpose. In one-at-a-time mode, you press and let go for each nail. In bump mode, you hold the trigger, and it fires each time the tip touches wood. Roof builders use the fast one. For careful work, people use the one-at-a-time mode.
What would go wrong without it
Now imagine the two controls are wired the wrong way round. You press what you think is the careful, one-shot trigger. But the tool fires all its nails.
The tool would look fine. Both triggers look like triggers. You would not see the mistake until it was too late. You could make the same mistake today. Your two sensors look the same, and the cables have no labels.
When two parts look the same, only the port tells them apart. So the port is not just a rule. It tells the robot which is which.
Before you build 1 min
This model has no step-by-step guide. The video below is not one either. It shows a finished machine working. Watch it to see what you are aiming for. Look closely at where the two switches sit. Then build from your teacher’s model, or from a photo of a finished one. You are not missing a page. There is no page.
The big idea — the port is the name 6 min
Two Touch Sensors. The same part, twice. Nothing on them says which is which.
the one in port 1
the one in port 2
This is the same part, drawn twice. There is no difference to find. The labels come from the cables, not from the sensors.
When your program says [1 v] wait until [bumped v], the Brick does not look for “the single-shot trigger”. It has never heard of it. It looks at port number 1. It reads whatever is plugged in there.
What you mean
What the Brick understands
“the single-shot trigger”
port 1
“the rapid-fire trigger”
port 2
So if you swap the two cables, the program still works. But it quietly swaps what the two buttons do. Everything still runs. There is no error message. Each button just does the wrong job. That is worse than a crash. A crash shows you where to look.
The port table, and why we bend it today
In this course, each sensor port has its own job. Touch goes on 1. Soon you will meet more sensors: the Gyro on 2, Colour on 3 and Ultrasonic on 4. We do this so one group’s program works on another group’s robot.
This model has two of the same sensor. So the second one has to borrow a port. It goes in 2. That is fine, because there is no Gyro here. But it is a choice you make. The rule that really matters is this one:
The program and the cables must match. Which port you choose matters less. What matters is that you write it down and plug it in that way.
▶The Touch Sensor, in fullPressed, released and bumped, and the trap with is-pressed inside a loop. Open it if any of that is not clear yet.Show meHide
ComponentSensing5 min
The Touch Sensor
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.
released
pressed
The red button out, and the same sensor with it pushed in. These two states are the entire output of this sensor — there is nothing in between.
Blocks reference
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.
Three different events
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.
when program starts
forever
if 1 is pressed? then
change count by 1
versus two hat blocks
1 when pressed
1 when bumped
0is pressed? in a loop0when pressed0when bumped
In a loop: 0 answers from one pressBumped: exactly one
Nobody is touching the sensor. All three programs are watching it.A finger presses the button. Watch the red button go in — a couple of millimetres is the sensor's entire movement.The finger is still down. The loop checking «is pressed?» has already run hundreds of times, and every one of them counted.The finger lifts. Only now does «bumped» count, because bumped means pressed AND released.One press. Three completely different answers.Finished. The same press, counted three ways.
released
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:
Pressed — the button is down right now. Good for “hold to run”.
Released — it is up again. Good for acting when somebody lets go.
Bumped — pressed and released. This is what you want for “click to start”, because it will not fire repeatedly while a finger stays down.
The classic bumper
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.
Why it matters
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.
Say this out loud before you move on: “The Brick does not know which sensor is which. It only knows which port.”
What’s in this build 4 min
Four parts have cables. That is the most of any model so far. And once again you have to tell us which motor it is. This model has no guide, and our notes do not agree about it.
Large Motor
Medium Motor
Which one is in your build? The Large Motor has a big red round end, and its body has steps. The Medium Motor is flat, with a small red bump on its end.
Part
What it is doing here
EV3 Intelligent Brick
Runs the program, and keeps the machine steady while it fires.
One motor — you decide which
Works the firing part, using the rubber band. Whichever motor it is, it goes in port A.
Touch Sensor ×2
The two triggers. They are the same part. The ports give them their jobs.
Label them before you plug them in
This takes thirty seconds. It will save your whole lesson.
Choose which sensor is the single shot one. It is usually the one nearest your firing hand.
Put a small piece of tape on its cable, near the plug. Write 1 on it.
Tape the other cable and write 2.
Now plug them into the ports with the same numbers. Now they can never get mixed up, because the cable tells you.
This is what real engineers do. The wires inside every machine you use have labels. The reason is the one you just met. The parts look the same, but they do different jobs. You cannot tell just by looking.
Ports — and the rule 4 min
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
The motor (works the firing part)
A
When there is just one motor, it goes in A, as it has all course.
Touch Sensor — single shot
1
Touch is always port 1 in this course.
Touch Sensor — rapid fire
2
Borrowed from the Gyro’s usual port. This model has no Gyro, and it needs a second number.
Check your own build now:
Motor cable in A. A letter.
The sensor you labelled 1 goes in port 1. The one you labelled 2 goes in port 2. Read the tape. Do not guess.
Press each button in turn. Say out loud which one you pressed. If you are not sure, go back and label them.
Load the bricks. Point the machine at an empty space. Never at a person or a window.
Connect the Brick 4 min
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.
▶How to connect the BrickUSB and Bluetooth, step by step, with a photograph of every screen. Open it if you have not done this before — or if pairing is not working.Show meHide
USB — the reliable one
Switch the Brick on with the dark grey centre button.
Cable into the Brick’s PC port — the small square socket beside the numbered ports, not one of the numbered ones.
Other end into the computer.
Bluetooth — name it first
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.
Name your Brick. On the Brick: Settings (the spanner) → Brick Name. Type something nobody else will pick, then press the tick. Every Brick is called EV3 until somebody changes it.
Turn Bluetooth on. Settings → Bluetooth. Tick Bluetooth and Visibility. Leave iPhone/iPad/iPod unticked.
Connect from EV3 Classroom. Click the Brick icon at the top of the programming area, find your Brick by name, and click Connect.
Say yes on the Brick. It asks “Connect?” with the computer’s name — choose the tick, then accept the passkey, which is already 1234.
Where to read it. The name sits in the bar across the very top of the screen, on every screen — so you can check which Brick you are holding at any moment without going into a menu. This one is EV3VE. A Brick nobody has renamed says EV3.Step 3, and the reason step 1 exists. Three Bricks in range — read the name before you click Connect. Pairing with the wrong one is not an error: it works perfectly, on somebody else’s robot.
Step 2.Bluetooth switches the radio on; Visibility is what lets the computer find you. With Visibility off your Brick works perfectly and simply never appears in the list.Step 4. Look at the Brick. It asks whether to accept and names the computer. Choose the tick.Then the passkey, already 1234. Press the tick again and you are connected.
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.
Four cables on one Brick is crowded. Keep them away from where the bricks fly out. Make sure none can be pulled out when the machine jumps. And it will jump!
Confirm the connection 2 min
This is the most useful check in the whole course. It is the one that catches a swap.
Look at the Brick icon. Is it connected?
Port A shows a motor. Which kind is it? Check it against section 5.
Ports 1 and 2 both show a Touch Sensor.
Now the important one. Press the sensor you labelled 1. Watch which tile changes. It must be tile 1. Then press the other one, and watch tile 2.
If the wrong tile changed, your cables are swapped. Swap them back now. It takes five seconds now. Later, it could take twenty minutes, while you blame your blocks.
You cannot spot swapped cables by looking at the model or the program. This screen is the only place it shows.
Change it and test 8 min
Make one change at a time, and guess what will happen before each run. Keep the firing line clear every time.
Change the single shot to 3 rotations. Guess how many bricks fly each click. On your build, does one rotation really fire one brick?
Change both [2] blocks to [1]. Guess what happens. Now one trigger does everything, and the other does nothing. The machine is not broken. You changed it in the program.
Put them back. Then swap the two cables at the Brick, and do not touch the program. Guess which trigger does what now. This is the bug from section 2. You just made it happen on purpose.
Swap them back, and change the speed to 40 %. Do the bricks still get out? A launcher that is too slow does not fire gently. It gets stuck.
Add a write [FIRING] at line (1) after start motor and a write [READY ] after stop motor. Now the machine shows which mode it is in.
Remember step 3. You did not change the program, but the machine acted differently. That is because half of what a program does depends on the cables, not the blocks.
A program is only half the machine. The cables are the other half. And cables give no error messages.
This is what you are building: the EV3 Machine Gun.
Build it 15 min
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.
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.
Make it move 10 min
Eight blocks. It is a long program, and every block is needed. Trigger 1 fires once. Trigger 2 keeps firing. Press it again to stop.
when program starts :: events hat
[A v] set speed to (100) % :: motors
[1 v] wait until [bumped v] :: sensors
[A v] run [clockwise v] for (1) [rotations v] :: motors
[2 v] wait until [bumped v] :: sensors
[A v] start motor [clockwise v] :: motors
[2 v] wait until [bumped v] :: sensors
[A v] stop motor :: motors
One rotation is one shot. Start motor has no end, so it keeps firing. These are the same two motor blocks you have used all course. Here they do very different jobs.
Go through it in the order the Brick runs it:
set speed to 100 % — a setting, not an action. It has to come first, before anything turns. You learned that in Lesson 36, the Gyro Launcher.
[1] wait until bumped — the program waits until the port 1 trigger is clicked.
run clockwise for 1 rotation — one shot. This block has an end, so the program waits for the shot to finish.
[2] wait until bumped — now it waits for the other trigger. Only the number has changed.
start motor clockwise — this block has no end. It keeps firing, and the program moves on while it does.
[2] wait until bumped — a second click on the same trigger, while the machine fires.
stop motor — and it stops.
What success looks like: click trigger 1, and just one brick flies. Click trigger 2, and lots fly out. Click trigger 2 again, and it stops. If trigger 1 fires lots, your cables are swapped. Go back to section 8.
Why bumped and not pressed? The same reason as the Timber Saw. With pressed, one long press on trigger 2 would count as start and stop. The machine would fire for a moment, then stop again.
Challenges 27 min
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.
Challenge 1
Make the machine say which mode it is in. A mode is the way it is working right now. The screen must say READY while it waits. It must say SINGLE while one shot fires. It must say AUTO while it keeps on firing. The word must change just as the action changes. Not before, and not after.
Challenge 2
Make the swap mistake on purpose. Then make it impossible. First, swap the two sensor cables. Write down just what a user would see happen. Then change the model so no one can make that mistake again. You could use labels, the way the cables run, or where the parts sit. You choose. Then say why your fix would still work for someone who has never read this lesson.
Challenge 3
Make the second trigger hold-to-fire. It fires only while the button is held down. It stops as soon as you let go. It does not need a second click. Then say which dropdown options you used on each sensor. Say why they are different.
Extra challenge
Build a machine someone else can pick up and use right the first time.
Give your machine to a group who have not seen it. Tell them just one sentence. They must fire a single shot, then a burst, with no mistakes. Your model must make its own controls clear. It can use labels, where things sit, what the screen says, or all three.
Plan on paper before you build. How will a stranger know which trigger is which? They must know before they press anything. Parts that look the same give them no clue.
Two questions when you show it. What tells the user which trigger is which? Would it still work if the labels fell off? And what does the Brick really know about your two sensors? Say it the Brick's way, not yours.