0

I am designing a "project" with 5 Arduino's (each with separate codes), communicating with each other using NRF24. All the 5 codes are different except they share a common header "MyHeader.h". The Arduino IDE forces the .ino filename to be same as the folder-name it is in. I currently have the following structure:

[parentfolder]
    MyHeader.h
    [arduino1]
          arduino1.ino
    [arduino2]
          arduino2.ino
    [arduino3]
          arduino3.ino
    [arduino4]
          arduino4.ino
    [arduino5]
          arduino5.ino

Now, if I use the following, I get an error that the IDE cannot find the header file:

#include "../MyHeader.h"

I am forced to use the above structure due to the IDE folder naming restrictions, whereas I would definitely like to have a structure like the following:

[parentfolder]
    MyHeader.h
    arduino1.ino
    arduino2.ino
    arduino3.ino
    arduino4.ino
    arduino5.ino

How should I organize my project so that I can include my header (without obviously duplicating the file inside every folder)? Thanks.

  • check out uecide.org – jsotola Nov 27 '19 at 3:46
  • put MyHeader.h in Documents/Arduino/libraries/MyHeader folder – Juraj Nov 27 '19 at 6:04
  • Does your filesystem support symlinks? – Edgar Bonet Nov 27 '19 at 8:33
1

The reason why you can't reference the parent folder of your sketch when compiling is because the Arduino IDE doesn't compile your sketch in that folder. Instead it writes out the files from the IDE itself into temporary files in a "build" folder and compiles them in there - so the parent folder then does not contain your header file because the IDE has no clue about that.

So you need to put your header file somewhere that the IDE does know about - and the simplest place is where the libraries are.

A library is nothing more than a header file in a folder (both named the same) with optional source and other header files. If you place your header in a folder in your libraries folder the IDE will see it as a library and you can include it into any sketch.

So you would have:

Documents/
    Arduino/
        libraries/
            MyHeader/
                MyHeader.h
    MyFancyProject/
        NodeOne/
            NodeOne.ino
        NodeTwo/
            NodeTwo.ino
        NodeThree/
            NodeThree.ino

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.