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.
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.
| Symptom | Likely fault class |
|---|---|
| expected ';' before ... | Missing semicolon on the line above |
| 'serial' was not declared | Capitalisation - C++ names are case-sensitive |
| expected '}' / else without a previous if | Braces 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 own | Pin mode does not match the wiring |
| Nothing lights, no errors at all | Pin number in the code does not match the circuit |
| Serial Monitor scrolls too fast to read | Printing every loop instead of on a change |
It will not compile
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.
Greenhouse controller Starting up
Braces that do not match
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.
Fan ON
The tank that empties itself
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.
Pump running Level: 40
The button is ignored
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.
Right code, wrong pin
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.
Five faults at once
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.
The flooded Serial Monitor
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.
Pressed Released Pressed Released
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.