Code Along - Chapter 9

Finding & Fixing Faults

Start from the symptom, name the fault class, fix one thing and re-test: compile errors, braces, = versus ==, pin modes, wrong pins and a flooded Serial Monitor.

Estimated time 60-80 minExercises passed 0 / 7

Start from the symptom

Guessing wastes time and adds new faults. Work the same way every time:

  • Write down exactly what happens - the compiler message, or what the board does and does not do.
  • Decide which fault class fits that symptom.
  • Change one thing, then run it again.

The code-correction assessment asks for exactly this: a fault inventory written before you fix anything, and an explanation of each fault class. This chapter practises every class on its own, then all of them together.

SymptomLikely fault class
expected ';' before ...Missing semicolon on the line above
'serial' was not declaredCapitalisation - C++ names are case-sensitive
expected '}' / else without a previous ifBraces that do not match
Compiles with a warning; the if always or never runs= used where == was meant
A button does nothing, or acts on its ownPin mode does not match the wiring
Nothing lights, no errors at allPin number in the code does not match the circuit
Serial Monitor scrolls too fast to readPrinting every loop instead of on a change
Exercise 9.1

It will not compile

Not started

This start-up message will not compile. Press Run, read the first error only, fix that one fault, and run again. There are two faults. Do not change the text in quotes.

Sample output
Greenhouse controller
Starting up
void setup() {
  serial.begin(9600);
  Serial.println("Greenhouse controller")
  Serial.println("Starting up");
}

void loop() {
}

Turn on JavaScript to edit, run and test this code in the browser.

Exercise 9.2

Braces that do not match

Not started

The fan message should say Fan ON above 30 degrees and Fan OFF otherwise, but the sketch does not compile. Find the brace fault. The test also tries a temperature of 25.

Sample output
Fan ON
int temperature = 31;

void setup() {
  Serial.begin(9600);
  if (temperature > 30) {
    Serial.println("Fan ON");
  else {
    Serial.println("Fan OFF");
  }
}

void loop() {
}

Turn on JavaScript to edit, run and test this code in the browser.

Exercise 9.3

The tank that empties itself

Not started

This pump check compiles, but it prints the wrong level - the sketch is changing tankLevel itself. Read the compiler warning, then fix it so an empty tank (level 0) switches the pump off and any other level keeps it running.

Sample output
Pump running
Level: 40
int tankLevel = 40;

void setup() {
  Serial.begin(9600);
  if (tankLevel = 0) {
    Serial.println("Tank empty - pump off");
  } else {
    Serial.println("Pump running");
  }
  Serial.print("Level: ");
  Serial.println(tankLevel);
}

void loop() {
}

Turn on JavaScript to edit, run and test this code in the browser.

Exercise 9.4

The button is ignored

Not started

The LED should light while the button on pin 2 is held. The button is wired from pin 2 to GND, like every button on this course. The code compiles and looks right, but the LED does not follow the button. Find the one word that does not match the wiring.

const int BUTTON = 2;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(BUTTON, INPUT);
}

void loop() {
  if (digitalRead(BUTTON) == LOW) {
    digitalWrite(LED_BUILTIN, HIGH);
  } else {
    digitalWrite(LED_BUILTIN, LOW);
  }
}

Turn on JavaScript to edit, run and test this code in the browser.

Exercise 9.5

Right code, wrong pin

Not started

No errors, no warnings - and the LED on pin 13 never lights. The circuit is correct and was checked with a multimeter. Compare the pin numbers in the code with the board, and fix the code to match the circuit.

const int BUTTON = 2;
const int LED = 12;

void setup() {
  pinMode(LED, OUTPUT);
  pinMode(BUTTON, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(BUTTON) == LOW) {
    digitalWrite(LED, HIGH);
  } else {
    digitalWrite(LED, LOW);
  }
}

Turn on JavaScript to edit, run and test this code in the browser.

Exercise 9.6

Five faults at once

Not started

A garage parking light: the LED on pin 13 must be on while the car switch on pin 2 is closed (wired to GND). This sketch has a fault from every class in this chapter.

Before you change anything, list each fault with its line number and fault class, the way the code-correction assessment asks. Then fix them one at a time, running after each fix.

const int SWITCH = 2;
const int Light = 13;

void setup() {
  pinMode(Light, OUTPUT)
  PinMode(SWITCH, INPUT);
}

void loop() {
  bool carPresent = digitalRead(SWITCH) == LOW;
  if (carPresent = true) {
    digitalWrite(light, HIGH);
  else {
    digitalWrite(Light, LOW);
  }
}

Turn on JavaScript to edit, run and test this code in the browser.

Exercise 9.7

The flooded Serial Monitor

DeeperNot started

This door monitor prints on every pass of loop(), thousands of lines a second, so nobody can read the log. Change it to print Pressed once when the button goes down and Released once when it comes back up - nothing else.

Sample output
Pressed
Released
Pressed
Released
const int BUTTON = 2;

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(BUTTON) == LOW) {
    Serial.println("Pressed");
  } else {
    Serial.println("Released");
  }
}

Turn on JavaScript to edit, run and test this code in the browser.

Try it on a real Uno

Download Five faults at once, upload the broken version and write your fault inventory on paper before fixing anything. Then wire a button from D2 to GND and prove the repair on real hardware.

Add one fault of your own - swap a pin number or delete a brace - and swap boards with a classmate. Time how long it takes each of you to find the other's fault by method rather than by guessing.