Skip to content
RoboLearn
Not completed

Debugging code

ProgrammingBeginner25 minDebug a broken robot ↓Before this: Functions

What you’ll learn

  • The three kinds of bug
  • A step-by-step debugging method
  • Tools: error messages, prints, and testing in isolation

Three kinds of bug

Kind What happens Example
Syntax The code won’t compile or run Missing ; or }
Runtime It runs, then crashes or freezes A loop that never ends
Logic It runs but does the wrong thing < instead of >

The method

  1. Reproduce it. Make the bug happen on purpose, reliably.
  2. Read the error message, including the line number (and the line before it).
  3. Print what’s really happening: sensor values, which if branch ran, variable values.
  4. Isolate: test the smallest piece alone (just the sensor, just one motor).
  5. Change one thing, test, and repeat.
  6. Explain it out loud (“rubber duck debugging”): describing the code line by line often reveals the bug.

Common robot bugs

  • = (assign) instead of == (compare) in an if.
  • Wrong pin number in code versus wiring.
  • Sensor logic upside down (HIGH vs LOW).
  • Motors wired backwards, so “left” turns right.
  • int used for millis().
  • A delay() so long the robot can’t react.

Finished this lesson?