Release 2017.5.20 / Update 2017.6.19

TIPS for finding bugs

You may unforcely fail to right code sometimes when you modify it. If the sketch isn’t long, it doesn’t matter. But the more big project makes it more difficult to find the bug.

On this article, I show you how I find it. These are TIPS for finding bugs when compile is OK.

Finding problems

If your miss is obvious, compiler finds it. But if it isn’t, the program works tentatively and hard to find.

I think reason that the program does not work well can be summarized into two points.

  • The program flow does not work as expected
  • Failing to calculate variable properly

As a conclusion, you have to find the wrong code yourself. But if you use Serial.print, it will be more easier to find the bug.

Wiring and Sample Sketch

If you use Arduino UNO, you can exchange SDA as A4 pin, SCL as A5 pin.

I expect to use OLED of 128×64 size and I2C.

Following sample works tact switch as “ON” display, variable resistor as value and gauge bar display.

Originally the numerical value and the bar will be displayed on the OLED if the switch is pressed “ON”, or by turning the variable resistor.

But it doesn’t, intentionally.

VR is disabled although display. So finding where to fix, use Serial.println.

Find freezing line

Basically, Arduino flow start from setup() function, then it loops loop() function. So at first, I make numbering print for doubtful code.

You can see it stopped before ‘D’ on Serial Monitor.

It tells you ‘BUTTON()’ function is suspicious. Then, find accurate code from BUTTON() function as same way.

You will see it stops before ‘C’.

while (digitalRead(SW) == true) gauge++;

I can assume this code goes wrong.

You have to find what is wrong at yourself in the end. But this way is much easier to find to where. By the way, this problem is caused of internal pullup.

while (digitalRead(SW) ==false) gauge++;

Now you can see tact switch works.

Well, here is another problem still. VR does not works yet.

As Serial Monitor returns B to E value, program seems to flow correct.

Check variable change

If the sketch doesn’t have unexpected stop, there could be wrong calculation for variables. So then, I make return of the variable to Serial Monitor. There is a variable ‘vr_val’ on this sample sketch, set Serial print return to end of line.

‘vr_val’ doesn’t change at all.

Just in case, set same returns on other lines.

You can assume VR_READ() function doesn’t work well.

And in the end, find it yourself.

As saying the answer, the return of VR_READ() is wrong. Modify the code to “return read_val;” or,

Now program works as I expected.

Make function and use preprocessor

As you know, this method is meaningless on this short sketch. But like my follow focus project, it is very convenient on the long code.

If you plan to have big project, you might get those debug code lines as comment out in advance.

But this is bothersome way. I recommend you to make a ‘debug function’ for the important arguments.

Then, by using ‘#define’ preprocessor, you can change entire displaying with ‘true’ or ‘false’.

And more, if you use ‘#if’ and ‘#endif’ preprocessor, it makes the sketch compact and be faster.

#if(Judgment formula)
  code~
#endif

I used this method on my Follow Focus Sketch. Check it if you are going to use.

DIY Follow Focus Sketch