If a program is misbehaving it is usually because of either:
- You think the program does something, but it is actually doing something (slightly) else.
- You forgot to take something into account
I will try to write some examples tomorrow. While they sound like errors only "stupid" people would make, it happens all the time even for the best people out there.
So one approach to the first issue is to ask another person to look trough your program, but without telling too much about what it does. When he have grasped an understanding of the program, make him tell you how it works. Did his explanation match exactly what you thought it did? If not, how and where did it differ? It is often just small differences, but they can have big consequences.
With the other issue, you can approach it by trying to do the program manually. Go through the program step-by-step in different situations, without thinking, without assuming how anything works. What happens and how does the program handle it? Are some situations handled in a way that doesn't make sense in that specific situation?
The problem with this approach together with robotics is that sensors receive external input and it is difficult to predict this exactly.
Consider a line following robot which takes input from a light sensor. If the amount of light in the room increases a lot, the light sensors reading becomes higher and it might have difficulty to differentiate between black and white.
Another aspect of the issue is that the robot will not have the same start conditions. Slight changes in the start position and direction will make the program run completely different from before. (The goal in robotics is to make the result of the program as similar as possible no matter how the conditions were before, now and in the near future.)
So since it is difficult to run the program by yourself, you can instead try to get it explain to you what it is doing. You can do this with sound, make it Beep in certain ways depending on the situation, or you can do it using the in-built display.
When you then run the program, try to notice which situations often happens when the program misbehaves. Did it do something which it shouldn't have done here? Or did it think something happened (and reacted based on this) which actually did not happen?