text: Q:
  
  How to use #define in a library I'm trying to write?
  
  I have written simple code to enable toggling of serial.print debug data.
    #define DEBUG
    #ifdef  DEBUG
      #define DEBUG3(x,y,z) Serial.print(x);Serial.print(" ");Serial.print(y);
       Serial.print(" "); Serial.println (z)
      #define DEBUG2(y,z) Serial.print(y);Serial.print(" ");Serial.println 
       (z)
      #define DEBUG(z) Serial.println(z)
      #define DEBUGln  Serial.println("");
    #else
      #define DEBUG3(x,y,z)
      #define DEBUG2(x,y)
      #define DEBUG(x)
      #define DEBUGln
    #endif
  
  Then I can just call DEBUG3("words",value,"words"); anywhere in the sketch and it will automatically print out the text, int, strings, etc with a single space between and automatically go to next line etc etc.
  Just define the correct number of fields to print and separate using commas.
  If I want to turn off debug then I just comment out #define DEBUG at the top.
  Now the issue is I don't want to copy in this code every time I want to use it, so it makes sense to make it a library so that I can just call something like:
  #include <SERIAL_DEBUG.h>
  #define DEBUG     (or //#define DEBUG) //to enable or disable
  
  The reason being that sometimes I might want to print up to 10 things on a single line (not very often but it could happen).
  This is my first library attempt and I'm having a hard time finding any information on how I would go about setting up the .h and .cpp files for this type of #define DEBUG(x) Serial.println(x) and the #define DEBUG (from sketch) parts.
  
  A:
  
  Anything in a header file that is included with #include is literally pasted into the file at the point of the #include.
  So anything in your sketch you want to place into a header file you can just place it in a header file and include that file at the same location where the original code was.
  So you can have a header file debug.h:
    #ifdef  DEBUG
      #define DEBUG3(x,y,z) Serial.print(x);Serial.print(" ");Serial.print(y);
       Serial.print(" "); Serial.println (z)
      #define DEBUG2(y,z) Serial.print(y);Serial.print(" ");Serial.println 
       (z)
      #define DEBUG(z) Serial.println(z)
      #define DEBUGln  Serial.println("");
    #else
      #define DEBUG3(x,y,z)
      #define DEBUG2(x,y)
      #define DEBUG(x)
      #define DEBUGln
    #endif
  
  And include that in your sketch at the right location:
  #define DEBUG
  #include "debug.h"
  
  Note that the #define DEBUG must come before the #include. Remember: the #include is replaced by the content of the included file, so after preprocessing you end up with exactly what you had before in your sketch:
    #define DEBUG
    #ifdef  DEBUG
      #define DEBUG3(x,y,z) Serial.print(x);Serial.print(" ");Serial.print(y);
       Serial.print(" "); Serial.println (z)
      #define DEBUG2(y,z) Serial.print(y);Serial.print(" ");Serial.println 
       (z)
      #define DEBUG(z) Serial.println(z)
      #define DEBUGln  Serial.println("");
    #else
      #define DEBUG3(x,y,z)
      #define DEBUG2(x,y)
      #define DEBUG(x)
      #define DEBUGln
    #endif
  
  The file can be in the same location as the sketch (in which case you should use #include "debug.h" or in a library folder (Arduino/libraries/debug/debug.h) in which case you should include it with #include <debug.h>. In the Arduino IDE it actually makes little or no difference which type of #include you use, but it does serve as a visual reminder that this is either a header included with the sketch ("") or an external library or system header (<>).
  
  