Level 2Reference

What changes this year

Last year you dragged blocks. This year you type. But you are not starting again — you already know everything this book is about.

That is the important part, and most books get it wrong. Typing code is not a new subject. It is the same ideas you used last year — do this, then that; repeat; if something, then something else — written in a different notation.

So for the first six sessions, every new thing arrives next to the block you already know. Block on the left, the line that does exactly the same thing on the right. By Session 6 the left column falls away and you write from an empty file.

The block you already know
The line that does the same thing
repeat 10
for (int i = 0; i < 10; i++) {
turn on pin 9
digitalWrite(9, HIGH);
}
This is what every new idea will look like when you meet it.
💡 Tip
If you are reading this and feeling like the blocks were easier — yes. For about three weeks. Then it flips, and it never flips back.

Why bother, when blocks worked fine?

  • Blocks run out. There is no block for the thing you will want to build by March.
  • Text is what the world uses. Every Arduino project online, every library, every example is written in C++. From this year you can read all of it.
  • It is faster once you know it. Typing a line beats hunting through menus for a block.
  • It is honest. The blocks were always turning into this. You were writing C++ last year without being shown it.

How this book works

Same shape as last year, with two additions.

  1. What we are making — the finished thing, and what it does.
  2. The idea — the new concept, explained before you build.
  3. Side by side — the block, and the line. Only in Unit A, while you are crossing over.
  4. Wire it up — a full-colour diagram. Copy it hole for hole.
  5. The code — a real C++ listing with line numbers, explained line by line.
  6. Try it — what should happen, and what to do when it does not.

Reading a code listing

Listings look like this. The numbers down the left are line numbers — they are not part of the program, they are so this book can say “look at line 4”.

How to read a listing
1void setup() {
2 pinMode(9, OUTPUT); // tell pin 9 it is an output
3 Serial.begin(9600); // open the line back to the computer
4}
5 
6void loop() {
7 digitalWrite(9, HIGH);
8 delay(1000);
9}
  • Orange words are built into the language: void, if, HIGH.
  • Blue words are functions — things that do something: pinMode, delay.
  • Green is a number. Grey italic after // is a comment, ignored by the board.
  • A highlighted line like line 2 above is the one being talked about in the text.
⚠ Careful
Type the listings out. Do not copy and paste them. The mistakes you make while typing are the entire point of Sessions 1 and 2 — they are what you learn to read and fix.

The rules of C++, in one page

You do not need to memorise this. Come back to it whenever something will not compile.

Every program has exactly two functions

The block you already know
The line that does the same thing
when green flag clicked
void setup() {
(the setup you did once)
// runs ONCE, at power on
}
forever
void loop() {
(everything inside it)
// runs FOREVER, over and over
}
Last year's green flag and forever block. They were always these two functions.

The four rules that cause 90% of errors

  • Every instruction ends in a semicolon. delay(1000); — the semicolon is how C++ knows the line is finished. Blocks did not need one because the block's edge was the ending.
  • Curly brackets come in pairs. Every { needs its }. They do the job the C-shape of a block used to do.
  • Capital letters matter. digitalWrite works. digitalwrite does not. DigitalWrite does not.
  • Round brackets hold the details. delay(1000) — the 1000 is how long, and it goes inside, exactly like the white oval in a block.
New words
FunctionA named block of instructions. setup() and loop() are functions.
CompileTurning your text into something the board can actually run.
Compiler errorThe compiler telling you it could not understand something. Not a disaster.
CommentText after // that the board ignores. Notes for humans.
Semicolon; — the full stop of C++.

The rules of this room

  • Nothing goes in the bin until it is checked against the card. One missing jumper wire costs another team a whole session next week.
  • Power off while you wire. Power on when two people have checked it. Almost everything that gets destroyed in this room is destroyed at the moment of switching on.
  • Motors never draw from the board. They get their own battery, and they share a ground. This is the one rule that protects the most expensive thing on your bench.
  • Write in your notebook every session, with the date. A project that failed with a good notebook scores higher than one that worked without.
  • If you break something, say so. Nobody is in trouble for a broken part. People are in trouble for a broken part that turns up next week in somebody else's hands.