#Define in INO file instead of included .h file

I am trying to use a library (conceptinetics DMX library) on multiple devices.
On my MEGA2560 devices, I'd like to use SERIAL1 instead of SERIAL0

In the conceptinetics.h file the lines below are present.

//#define USE_DMX_SERIAL_0
#define USE_DMX_SERIAL_1
//#define USE_DMX_SERIAL_2
//#define USE_DMX_SERIAL_3

I obviously just uncomment the correct line and away I go. Buuuuuut I want to work on multiple arduino boards, and they don't all have SERIAL1 ports. So rather than edit my include file over and over, is there a way to make that declaration in the INO file instead?

I tried just commenting all 4 lines out and adding the correct definition to the INO file, but it hasn't worked. Compiler throws tons of errors. I assume this is a limitation of the IDE as opposed to the actual include file itself. Is there a way to do what I'm trying to do?

You can do a conditional compile based on board selected:

get in contact with the author of the library and ask for a change request.
Explain, that you would like to hand over the serial interface to the library in your user sketch.

Try use your include file with


#if  defined(__AVR_ATmega2560__)
#define USE_DMX_SERIAL_1
#endif

Does it work?

P.S. Wrong person replied. Sorry

No, it is not a "limitation of IDE", it defined by language rules. To be used correctly, the #DEFINE setting should be part of the DMX library. The conceptinetics.h file is included to the library source, so the #DEFINE will work. The .ino file is not included to the library code so the #DEFINEs in it does not affect the compilation process.

Without editing library source code - no.

You could use Serial1 on the boards that have it and Serial on the rest:

#ifdef Serial1
#define USE_DMX_SERIAL_1
#else
#define USE_DMX_SERIAL_0
#endif
//#define USE_DMX_SERIAL_2
//#define USE_DMX_SERIAL_3

Ok thank you everyone. I will go with the ifdef option for now I guess.
That will work for this particular bit of project and save me the flip flop editing.

I was under the impression that the .h files were included into the ino file and then the code was compiled. So I thought I should be able to move the definition. Is the include file compiled seperately? If so, I don't understand how I can end up with variable naming conflicts (other projects, not this one)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.