Introduction: SERIAL DEBUGGING WITH ARDUINO
Nevertheless are available some development platforms to write arduino code, is difficult to effectively debug software when it runs on external devices, especially if you have a simple Arduino Uno, which is not equipped with some special features required to seriously debug your code, available only to more advanced Arduino(s).
So, not afraid to reinventing the wheel, I wrote a simple Debug class which have some useful function to print out text and data via the Serial port. In addition is possible to break/restart code execution in order to improve the debugging process.
I believe this approach is not rocket science, but I think that could help beginners to write better code. In addition is fully extensible and when you are ok with the debug, is very easy to disable debug and go back with clean and optimized code ready to be deployed in your projects.
Step 1: Debug Class Implementation and Basic Arduino Setup
If you want to enjoy the break/continue capability provided by this class, you need to use a pushbutton and attach the pushbutton pins as the following: one to GND and the other to a free digital pin. Button will be declared as INPUT_PULLUP, so no resistor is necessary to make it work.
To activate debugging features to your project you simply need to:
1) add Debug.h and Debug.cpp to your project
2) include Debug.h in every C++ file you want to debug
3) enter in Debug.h file and comment/uncomment the line with the #define DEBUG 17 preprocessor directive. Note that 17 is the number of the digital pin I normally use for the break/continue pushbutton, obviously you need to inform the number corresponding to the pin you want to use in your particular project. If you are not interested in break/continue features, please use 255 as (magic) pin number instead.
After that, you have at disposal the following directives, I will explain soon the parameters:
DBGBEGIN(speed) start the debugging class and the serial port, must be the first instruction in setup()
BRK(...) print some data on the serial according to the given parameters AND break execution
DBG(...) print some data on the serial according to the given parameters
DBG can be used in 4 different overloaded variants, in all variants is printed a preambol informing the file name, function and line number in which DBG has been invoked. After that arguments as and are sent to serial according to their type. It's easier to experiment than to explain, therefore I encourage you to write a simple sketch and start experimenting checking output on the serial monitor. I'm particularly proud of the last one, which writes a buffer both as string and hex.
- DBG(const char *title)
- DBG(const char *title,const char *data)
- DBG(const char *title,int32_t data)
- DBG(const char *title,int32_t size, const uint8_t *data);
BRK can be used in 4 overloaded variants + 1 without args, which have the same meaning than DBG, not only they print data on serial, they also break execution until the pushbutton in pressed. Also like DBG, in all variants is printed a preambol informing the file name, function and line number in which BRK has been invoked. After execution stop, you can resume execution by pressing the pushbutton associated to the pin informed in the DBGBEGIN directive.
- BRK(void)
- BRK(const char *txt)
- BRK(const char *t1,const char *t2)
- BRK(const char *t1,int32_t n)
- BRK(const char *title,int32_t size, const uint8_t *data);
From technical standpoint the Debug class is implemented very simply using overloading. Usage of preprocessor directives permit to wipe out the entire debug code when is time to go in production. That's it. Hope you enjoy.




