1. Debugging Makes Us Better Programmers
When things go wrong with your program it can be very frustrating. But trying to figure out what is wrong and then fixing it will help make you a stronger programmer. In addition, the debugging skills you gain in learning to program can also help you in your other math and science classes, where you often have to figure out and solve problems.
Debugging a program can be challenging enough when you're working with virtual, on-screen robots. But when you're also working with hardware devices like Arduino boards it's even more challenging because there are more things that can go wrong. To help you out, we list a series of steps below that are helpful to think about when trying to identify the problem.
2. General Advice
The Step button: Remember to use the Step button to run through your code step by step. Doing so can help identify where the problem might be.
Print key values while running: If your code uses variables, temporarily put print or messageBox blocks in the code at various points to print out the value of the variable(s). This allows you to check that the values are correct and/or changing correctly as the program runs.
3. Advice for Arduino Debugging
NOTE: These tips are for when an Arduino board is connected directly to a computer. If you are using an Arduino connected to a Linkbot robot, see the lesson on Robotic Technologies Debugging Tips.
More potential failure modes: When you combine Arduino boards, sensors, and wires it introduces more potential "failure modes." Here are some things to check when things aren't working.
Check the ChDuino software connection: If you run a program that is supposed to do something with the Arduino (such as blink an LED), but nothing happens, the first thing to check is whether the Arduino has lost connection with the computer. One indicator of a good connection is that the small flashing light on the Arduino is flashing. Then go to the ChDuino software and make sure that the connection is still "green" and other indicators are functioning, as described in the lessons on Software Setup for Arduino (Windows/Mac) and Software Setup for Arduino (Chromebook). If the connection has been lost, simply use the Disconnect and Connect buttons to restore the connection. (Or Delete the connection and Scan for the Arduino again, before reconnecting.)
Use ChDuino's manual control options: Depending on the circuit you are working with, you can also use ChDuino's manual options that control the high/low states of the digital pins to test that things are working.
Check the Arduino-computer cable: If you are using a USB cable to connect the Arduino to the computer, check that it is plugged in properly on both ends. If it is, and you have an extra cable, try swapping them. Occasionally (but not very often) a cable can go bad.
Check connections and wires: If the Arduino seems to be connected properly and running, check that your connections are correct. It is easy for wires to become loose, so make sure that the wires are fully plugged in to the Arduino and the breadboard (if using one). Sometimes the silver plugs on the wires are covered with a plastic material, so using your fingernail to scrape it off gently can help make a better connection.
"Off by one" errors: It's also very easy to get "off by one" errors, where a wire is one socket/pin over from where it should be on the Arduino board or breadboard. So double-check everything is connected as it should be.
Bad wires or sensors: Occasionally the hardware itself may be the problem. For example, the wires are relatively thin and can have a broken connection at one end or internally. So swapping out wires is one thing to try. This also applies to any sensors being used. So if you have another sensor of that type, try switching them.
Bad Arduino board: The swapping advice even applies to the Arduino board itself. It doesn't happen very often, but you can end up with a bad board or one that somehow gets corrupted. If you are trying to make a more complicated circuit work, try running some simpler activities that use an LED module or a sensor such as an ultrasonic sensor.
Restart the computer: If all else fails, restarting the computer can reset everything and allow you to start fresh.