Is there a workaround for scope or ino load order?

I've declared all my constants in a seperate tab

When compiling I get 'myconst' was not declared in this scope.

To verify I moved the declaration on top of sketch and it funtion as planned.

Then I had the idea to #include the ino file on top of the sketch which work, but now I get 'myconst' previously defined.

Are there a workaround for this? I like to have all the contants in seperate file with easy access from the IDE.

Assuming (because you have not told us) that you have a myProject.ino file inside the myProject directory and also a myData.ino file in the same directory the Arduino IDE first loads the principal file and then loads the other files in alphabetical order.

Variables defined in an earlier file are visible to code in a later file, but not vice versa. So it may make sense to swap the code between your two files.

I normally put my extra code etc in a .h file rather than a .ino file and I have #include "myData.h" in the principal file.

...R

Yes you are correct, multiple .ino files in a project folder with project name which the IDE opens up as Tabs.

I've used const.h files earlier but for this project I like the convenient of having the const file in the same IDE for quick refrence.

How does the logic behind multiple declaration of <Wire.h> work? I where hoping me declaring the .ino file already in the project folder would work out similar to this.

The first file (with your project name) will always be the main sketch. It however does not not have to contain setup() and loop(). You can dump your constants in there. And in another file add setup() and loop().

But using .h and .cpp files is a lot easier as you don't have to think about what the IDE might do wrong.

StillNotWorking:
I've used const.h files earlier but for this project I like the convenient of having the const file in the same IDE for quick refrence.

You can do this just as you would have multiple .ino files as tabs in a sketch. The only difference is that when you name the tab add the .h extension.

StillNotWorking:
How does the logic behind multiple declaration of <Wire.h> work? I where hoping me declaring the .ino file already in the project folder would work out similar to this.

The header file has an include guard which prevents multiple inclusions from the same translation unit:

#ifndef TwoWire_h
#define TwoWire_h

...

#endif

Excellent! Thanks guys. Didn't know IDE did other extension than ino. I've probably buried my .h files in lib folders on earlier tests.

Now then, this solution with inlcude .h to alter order of files has one more issue though. It need full path to project directory.
Is there a macro one could use to insert active path to the include — for shared projects?

You don't need the full path. Just use the quotes syntax:

#include "const.h"

Thanks pert.

Ha ha, this is funny. I've now gone full sircle. The variable the constant first where missing for are now missing the variable declaration itself as the function using it are loaded before the declaration.

But again, thank you all. I have the workaround I was looking for. Its for me to implement.

StillNotWorking:
Now then, this solution with inlcude .h to alter order of files has one more issue though. It need full path to project directory.

When you need to share .h files between 2 or more projects that is one of the most irritating shortcomings of the Arduino system IMHO. I am not aware of any simple way to make it happen. It would be soooo nice if the IDE would recognize relative path references.

For my own programming I have written a simple Python program to compile and upload using the command-line IDE. The Python program converts relative references to absolute references. I normally use the Geany but the Python program will work on its own.

...R

Is it a problem caused by the Arduino IDE's placement of the automatically generated function prototype? If so, you can manually add the function prototypes wherever you like them and the IDE will not generate them.

sterretje:
But using .h and .cpp files is a lot easier as you don't have to think about what the IDE might do wrong.

Also, by using .cpp files rather than all .ino files you keep the type checking alive across modules. Are you using the extern data qualifier in the other modules that use a previously-defined variable in your .h file?

econjack:
by using .cpp files rather than all .ino files you keep the type checking alive across modules

I've seen you say this a couple of times but it's not clear to me what you mean. The .ino files are turned into a .cpp file before compilation so I don't understand what the difference could be regarding type checking. Could you provide an example of this?

Robin2:
When you need to share .h files between 2 or more projects that is one of the most irritating shortcomings of the Arduino system IMHO. I am not aware of any simple way to make it happen. It would be soooo nice if the IDE would recognize relative path references.

Maybe sym-linking will do the trick? Never tried it, not using linux at this moment. And being naughty, one can dump it with the other libraries in the installation directory :slight_smile: But re-install of the Arduino software will possibly cause problems :wink:

Or go hardcore with makefiles.

Linux users usually know what they're doing :smiley: