Pages: [1]   Go Down
Author Topic: Using Defines in Sketch across Files  (Read 271 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

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
#endif
(in other.h)

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


Sam

Logged

UK
Offline Offline
Shannon Member
****
Karma: 183
Posts: 11154
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it possible?

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

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46026
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 183
Posts: 11154
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley-eek-blue
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: