Level 2Unit A · Session 2
2

Unit A · From blocks to text

Reading an error without panicking

What we are making: nothing. Today you break things on purpose and learn to read what the computer says back.

Why a whole session on this

Blocks only fitted one way. Text does not, so from now on you will get errors — and so does everybody, including people who have done this for twenty years. The difference is only how fast they read one.

💡 Tip
The red text is frightening for about one session. After today it will be the most useful thing on the screen, because it tells you exactly where to look.

How to read one

An Arduino error looks like this. It is not a sentence, it is four pieces of information.

A real error message
sketch_oct12a.ino: In function 'void loop()':
sketch_oct12a.ino:7:3: error: expected ';' before 'digitalWrite'
digitalWrite(9, LOW);
^~~~~~~~~~~~
  • 7:3 — line 7, character 3. Start looking there.
  • error: expected ';' — what it wanted. It wanted a semicolon.
  • before 'digitalWrite' — where it noticed. The semicolon is missing from the line before this one.
  • The ^~~~~ arrow points at the exact spot.
⚠ Careful
The golden rule: the error is usually on the line above the one it names. The compiler only notices something is missing when it reaches the next thing.

Break it on purpose

Start from your working Session 1 program. Break one thing, compile, read, fix. Then the next.

Break 1 -- the semicolon gone from line 5
5void loop() {
6 digitalWrite(9, HIGH)
7 delay(1000);
8}
Break it like thisIt saysWhich means
Take a semicolon off the end of a lineexpected ';' before 'delay'Look at the line above the one it names
digitalWrite → digitalwritenot declared in this scopeI have never heard of that word. Nearly always a capital letter
Delete the last } in the fileexpected } at end of inputA bracket is missing somewhere. Count them

Three breaks. Do them one at a time and fix each before the next.

⚠ Careful
Break 3 is the nastiest and worth doing slowly. The error points at the last line of the file, which is nowhere near the bracket you deleted. A missing bracket is always reported at the end, because that is where the compiler finally runs out of file.

The error hunt

Now a game. Your teacher will give each team three broken programs. Fix all three. Time it.

  1. Compile. Read the first error only — later ones are often caused by the first.
  2. Go to the line number it gives you.
  3. Look at that line, then the line above it.
  4. Fix one thing. Compile again. Repeat.
⚠ Careful
Fix one thing at a time and recompile. Changing three things at once means you no longer know which one was the problem, and you may have added two new ones.

Build the class error list

As a class, collect the errors that actually came up today and write them on a poster, in your own words. It stays on the wall all year, and it will be the thing you reach for most.

What you seeWhyWhat to do
expected ';' before ...Missing semicolon.Look at the line ABOVE the one named.
was not declared in this scopeSpelling, or a capital letter.Check the exact spelling. digitalWrite, not Digitalwrite.
expected } at end of inputA curly bracket is missing.Count your { and }. There must be the same number of each.
stray '\' in programA wrong character, often a smart quote from copying.Retype the line by hand.
Port not found / upload failedNot a code error at all.Check board, port and cable.

What you learned

  • Errors are specific, not angry.
  • Read the first error, go to the line, look at the line above.
  • Fix one thing, then compile again.
  • Everyone gets these. Constantly.
Challenge optional — only if you finish early
  • Break the program four different ways on purpose. Write down each error message and what caused it.
1. The same program, both ways