Arduino - How to split project in multiple file

Good Morning Friend,
I've done a big project with Arduino more than 20 pages of code, i would like separate this code for maintain it better because no it's difficult troubleshoot etc etc.

I've try to look in internet and on the forum first for discover an example to follow in order to outgoing this,
but i didn't find nothing.

Is there someone that can help me ?

Thanks in advance for your support,

Regards,
Gnux

Just create additional .ino files in the project folder. They should be opened automatically and will all be compiled into the main project. To add a new file via the IDE, use the arrow on the right-hand upper corner and click "New Tab". A "tab" is a file in the project and is added to the folder automatically.

And keep in mind that the Arduino IDE reads the extra .ino files in alphabetical order and global variables in one file can only be seen in later files. So it can be a good idea to put the global variables in the main project .ino file.

...R

I am facing that same problem, and have discovered that there are magic incantations which the IDE does or does not apply to files. In an attempt to simplify the construction of tiny sketches by beginners, it does unknown transformations of you code under unknowable conditions, which can result in spurious errors in what appear to be valid programs. I just split my large source into six files (it will probably be closer to 20 before I'm done) and it no longer compiles. I have to do "black box" experiments to figure out how to either evade the damage caused by these transformations, or compensate for the unknown transformations which are not being done.

While I have no argument against making things easier for beginners, it is also essential to support people like me, who have had 50+ years experience as programmers (and in my case, 20+ years with C++).

The simple, obvious, and (for any other environment) correct mechanisms fail in the Arduino IDE.
joe

it no longer compiles

What errors do you get ?

By the way. If you do not like the Arduino IDE or do not like the concept of a hidden main() function repeatedly calling loop() then just write your own main() function and have it #include any files you want in any order.

flounder:
I have to do "black box" experiments to figure out how to either evade the damage caused by these transformations, or compensate for the unknown transformations which are not being done.

While I have no argument against making things easier for beginners, it is also essential to support people like me, who have had 50+ years experience as programmers (and in my case, 20+ years with C++).

Just go with the flow and it's easy.

I don't agree that it is essential to support experts. Experts should find it very easy to accommodate themselves to a simplified system whereas beginners would find it very difficult to work with an "expert" system. If C/C++ wasn't so damned complicated to start with there would be no need for the simplifications that the Arduino system uses.

...R

1 Like

Hi every body,
I've always develop code using pascal, c++, visual studio ... etc etc ...
now the best thing i think is have an example follow at least for start ... in this way we can save time ... in my case I've 20 pages of code ... now i would like to split in order to obtain the maximum of efficency ... I think that this forum is very useful thanks in advance for the support...

now i would like to split in order to obtain the maximum of efficency .

Please feel free to go ahead.

Have you got a question ?

gnusso:
Hi every body,
I've always develop code using pascal, c++, visual studio ... etc etc ...
now the best thing i think is have an example follow at least for start ... in this way we can save time ... in my case I've 20 pages of code ... now i would like to split in order to obtain the maximum of efficency ... I think that this forum is very useful thanks in advance for the support...

Is the information in Replies #1 and #2 in answer to your first Post not suitable?

...R

flounder:
I just split my large source into six files (it will probably be closer to 20 before I'm done) and it no longer compiles. I have to do "black box" experiments to figure out how to either evade the damage caused by these transformations, or compensate for the unknown transformations which are not being done.

While I have no argument against making things easier for beginners, it is also essential to support people like me, who have had 50+ years experience as programmers (and in my case, 20+ years with C++).

See: How to avoid the quirks of the IDE sketch file pre-preprocessing