Sprinkling Serial.println() debugs within libraries.

i'm still slowly learning the inner workings of libraries to troubleshoot a project that uses one, but to speed up getting to a solution on that project, would just like to ask if i could simply insert Serial.println("you are at xxxx"); into those libraries without any adverse reactions.

i would insert it into the library.cpp file, right ?

sometimes you can do that but be aware that serial prints are really slow in terms of speed (from cpu point of view) Also some libs disable interrupts and then print won't work at all.

Another (better?) way to debug is to make a pin high/low and connect a led to it (or use pin 13) . Much faster and can be used even in an interrupt.

robtillaart: sometimes you can do that but be aware that serial prints are really slow in terms of speed (from cpu point of view) Also some libs disable interrupts and then print won't work at all.

Another (better?) way to debug is to make a pin high/low and connect a led to it (or use pin 13) . Much faster and can be used even in an interrupt.

won't be able to use pin 13 since this is SPI related. i was hoping to sprinkle a few debug prints in one go, instead of a non-identifying LED blink one location at a time. anyway, thanks for the reassurance - speed won't be a problem because the code simply hangs and i'd just like to see where in a particular function of the library it stops - i'm troubleshooting what's going on in the circuit and not actually in the library itself. i believe it's a hardware problem but i can't locate it - so i thought i'd use the software to help me pin it down.

The library needs to include Arduino.h if it is not already doing it. The declarations of Serial objects are there.

You can wrap pre-compiler directives around Serial.print (it does get weird):

In library.h on line 1 do: #define my_sprinkled_serial_debug

In library.cpp at the proper locations:

ifdef my_sprinkled_serial_debug

Serial.println("Stuff to print");

endif

This way you can disable all the debugs when you comment the #define in the .h file

liudr: The library needs to include Arduino.h if it is not already doing it. The declarations of Serial objects are there.

thanks, good tip to keep in mind - i'd probably spend quite some time before realising libraries are not necessarily getting what the main code 'gets' (as includes, or as built-in Arduino code)

liudr: You can wrap pre-compiler directives around Serial.print (it does get weird):

thanks - yeah, i've seen those "ifdef DEBUG" used - now i know what they're called ! :D

These directives are nice for simple situations but become convoluted in more complex situations, such as you may want options of "Brief debug info", "Intermediate level of debug info", or "Verbose debug info", all done with #define

heh-heh, i wouldn't say "weird" but i can certainly imagine your scenario and getting "weirded out" by all the '#' in the code all over the place !

when i first saw it in action, i actually thought it was some Arduino (or C++) keyword and would be used like;

#define DEBUG 1 // or 2 or 3, etc !

later i realised it was boolean and had to be commented out to switch it off !

Yep, you can define something without a value for substitution or define something with a value for substitution. Afterall, the directives are plain substitutions.

If you're determined to use prints, don't forget to use the F() macro, or you could make things much worse, very quickly.