Lesson 14: Debugging Robot Code

Debugging is the process of finding out why the robot is not doing what you expected. Good programmers are not people who never make mistakes. Good programmers are people who can slow down, collect evidence, and make one useful change at a time.

Start With the Symptom

Before changing code, describe the problem as specifically as you can. "The arm is broken" is not specific. "The arm motor moves down but never moves up when button 5 is pressed" is specific enough to investigate.

Use Prints Carefully

A simple way to inspect your code is to print values while the robot runs. Print the joystick value, button value, sensor value, or state variable that decides what the robot should do.

public void teleopPeriodic() {
    double speed = joystick.getY();
    System.out.println("Drive speed: " + speed);
    leftMotor.set(speed);
    rightMotor.set(speed);
}

Do not leave dozens of prints running forever. Too much output makes it hard to see the useful information. Add a print, learn from it, then remove it or turn it into a real dashboard value.

Check Hardware and Wiring

Software is not always the problem. Before rewriting a class, check the simple things:

Change One Thing

The fastest way to get lost is to change five things and then test. If the robot works, you do not know which change fixed it. If it still fails, you do not know which change made it worse. Make one change, test, and write down what happened.

Practice

Create a small bug on purpose: invert one joystick axis or use the wrong button number. Then use print statements and wiring checks to prove where the bug is. Practicing debugging when you are calm makes it much easier during a match timeout.