Suggested implementation of debug print on serial

#ifndef MYTYPEDEF
#define MYTYPEDEF
#include "string.h"

#define COMPILEDEBUG  //dont define this if you want to remove the debug print from the compilation

char *getSourceFileName(char *filnam);

#ifdef COMPILEDEBUG
#define debugMain 0x01
#define debugDS18B20 0x02

#define DEBUGPRINTMain if (debug & debugMain)Serial.printf((char *)"\r\n%s line %d : ", getSourceFileName((char *)__FILE__), __LINE__);if (debug & 0x01) Serial.printf
#define DEBUGPRINTDS18B20 if (debug & debugDS18B20)Serial.printf((char *)"\r\n%s/%d : ", getSourceFileName((char *)__FILE__), __LINE__);if (debug & 0x02) Serial.printf
#else
#define DEBUGPRINTMain  //
#define DEBUGPRINTDS18B20 //
#endif

#endif

Usage:

  1. In the source file
    int debug = 0; //somewhere once then extern elsewhere
    "DEBUGPRINTMain("this is example %d\r\n", 1);" without the quotes
  2. The debug print will not happen until debug has the the 0-bit turne on : 0x01 or 1
  3. To enable the second example turn on the 1-bit: 0x02 or 2
  4. Debug is enabled by viewing the Serial Monitor window and clicking on the line that says "Message" then type "debug=1 " or whichever you widh to enable. Use "debug=255" to enable all.
  5. To remove the debug prints from the compilation just dont define "COMPILEDEBUG".
  6. This does not replace the debug options providd by the Arduino IDE in the tools menu, it just allows evaluation of progress/variables when no crash occurs.
  7. getSourceFileName(..) is not required, use FILE instead, if you want the whole path to the source file - sometimes this is a good idea.
  8. A version of getSourceFileName(this could be optimised but I didn't bother as the code will not be in release version0:
#include "string.h"

char filenam[100];

char *getSourceFileName(char *filnam) {
  int i=0;
  strcpy(filenam, filnam);
  for (i=strlen(filnam); i>0; i--) {
    if ((char)filnam[i] == (char)'/') {
      i++;
      break;
    }
  }

  strcpy(filenam, (char *)&filnam[i]);
  strcat((char *)filenam, (char*)":  ");
  return(filenam);
}

Welcome to the forum

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

i define a debug variable

unsigned debug   = 1;

it can be hardcoded as above or changed in a function that reads commands thru the serial monitor

void
pcRead (void)
{
    static byte chip = 0;
    static byte port = 0;
    static int  val  = 0;

#define None    -1
    if (Serial.available ()) {
        int c = Serial.read ();

        switch (c)  {
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            val = c - '0' + (10 * val);
            if (32 & debug)
                Serial.println (val);
            return;

        case ' ':
            break;

        case 'D':
            debug = val;
            break;

        case 'd':
    ...

and used as follows

void i2cWrite (
    byte    chip,
    byte    port,
    byte    val )
{
    chip += chip < Chip20 ? Chip20 : 0;

    if (8 & debug)
        printf ("   %s: c 0x%02x, p %2d, v 0x%02x\n",
            __func__, chip, port, val);

    Wire.beginTransmission (chip);
    Wire.write (port);
    Wire.write (val);
    Wire.endTransmission ();
}

The defines makes it very easy to:

  1. In the Arduino IDE debug window, change the debug level by entering "debug=x" where x is the accumalation of the bit values assigned in the define.

  2. Compile out the debug code for the release version.

  3. In multi source file projects it might be useful to also display the source file name where the display is.