Using Defines in Sketch across Files


Is it possible to create a define in your sketch that will be used by other files in your library? For example, I would like to set a define such as #define FIRST in my sketch. Then I would like this to be used to define a secondary value in another file stored in the libraries directory.

As in

#define FIRST
#include <other.h>
(in my sketch)

#ifdef FIRST
#define VALUE 100
(in other.h)

So far I have not been able to get this to work. Is it possible?


samcx2: Is it possible?

Yes, and what you've tried looks correct. In what way does it not work?

Where is other.h, try changing it to #include “other.h”

You can not define a term in the sketch and have it used in the header file when that header file is used in another file. The existence (and value, if any) of FIRST will be seen in the sketch. If it is referenced in the header file, it will affect any code in the header file (there should not be any).

If that header file is included elsewhere, as in another source file, FIRST will not be defined (or valued).

If FIRST is defined (and valued) in the header file, then it will affect the sketch and other source file it is included in.

tobyb121: Where is other.h, try changing it to #include "other.h"

Good point. In at least some versions of the IDE, #include statements that cannot be resolved are silently ignored. :fearful: