Multi file arduino sketch

In the same directory as the main file. (You should use 1 directory per project)

Could you please point me to the documentation/instructions for including an external file (one I wrote) in an open sketch?

Could you please point me to the documentation/instructions for including an external file (one I wrote) in an open sketch?

What kind of file?

Typically, one creates extra .ino files to contain functions that the sketch calls. All .ino files in the sketch directory are opened in separate tabs when the sketch is opened, and are all combined into one .cpp file for compilation.

You can also create header files (.h) and #include them in a sketch. Then, you create a correspondingly named .cpp file in which the functions defined in the header file (if there are any) are implemented.

Thank you for that clear answer! After a good deal of more or less random effort, something like that understanding came late last night. I was looking for something more complicated, and some sort of directive to explicitly include such files at some specific points in the main program. All, apparently, unnecessary concerns.

Thank you.

The same just happened to me!
Thanks!!!

I tryed as in java to do import of a class that I defined in a tab.

PatoPato:
I tryed as in java to do import of a class that I defined in a tab.

If you follow conventions for programming in C++ you will have a much better chance for success.

PeterH:

PatoPato:
I tryed as in java to do import of a class that I defined in a tab.

If you follow conventions for programming in C++ you will have a much better chance for success.

Sure, but my expertice is in java, and I do it without noticing. Also I "jump" from java to arduino, to processing, and java again... and so one.

Yes,

I need to find the Sketch->Add INO file, menu option.
It seems to be missing.

'Add new ino file', 'new ino file' ?.

I believe beginners should use Atmel Studio 6, before moving onto something as complex as the arduino environment.

penguinman76:
Yes,

I need to find the Sketch->Add INO file, menu option.
It seems to be missing.

'Add new ino file', 'new ino file' ?.

The little button with the 'v' on the RHS is a menu with New Tab as one of the options.

I believe beginners should use Atmel Studio 6, before moving onto something as complex as the arduino environment.

This has to be a wind-up / troll

...R

PaulS:
What kind of file?

Typically, one creates extra .ino files to contain functions that the sketch calls. All .ino files in the sketch directory are opened in separate tabs when the sketch is opened, and are all combined into one .cpp file for compilation.

You can also create header files (.h) and #include them in a sketch. Then, you create a correspondingly named .cpp file in which the functions defined in the header file (if there are any) are implemented.

I want to split my code into 2 files. The main sketch calls a function in another file. In plain C++, I would create another pair of .h/.cpp files to put the function declaration and implementation in. For arduino, should I go with this approach, or instead create a separate .ino file?

Either way, where exactly do I specify that I want the .cpp file to be used while compiling? I can do a "#include "myfile.h" but how do I specify the .cpp file for the compile command line?

1 Like

The IDE takes care of it at the moment that you include the .h file.

sterretje:
The IDE takes care of it at the moment that you include the .h file.

So what's the purpose of a separate .ino file, compared to a .cpp file?

Just my thoughts.

.ino is simpler as one only needs one additional file instead of two (polluting the 'tabs' in the IDE). Also, people
new to programming might not quite understand .h and .cpp files while .ino sounds familiar.

I prefer .h and .cpp as I'm an 'old' programmer.

The Arduino IDE automatically includes all the .ino files. It starts with the .ino with the project name and then takes the others in alphabetical order. This means that a file can refer to things in in any file that was merged before it but a file cannot refer to stuff in a file that is merged after it.

...R

Robin2:
The Arduino IDE automatically includes all the .ino files. It starts with the .ino with the project name and then takes the others in alphabetical order. This means that a file can refer to things in in any file that was merged before it but a file cannot refer to stuff in a file that is merged after it.

...R

Yes but I am trying to ask the origin of a new .ino in the first place. Can you answer this: You have one main .ino file. Now you want an extra file which has some functions.

  1. .h/cpp

or

  1. another .ino

Whichever you prefer. The end result (a working program) will be the same and it will have no idea how it was created.

...R

  1. .h/cpp

or

  1. another .ino

One problem with another ino file is that the IDE sews all the .ino files together to produce a .cpp file. In what order? If you don't KNOW, don't let the IDE do your work for you.

Ok, thank you all.

PaulS:
In what order? If you don't KNOW, don't let the IDE do your work for you.

See Reply #16

...R

works for me:

  • aditional files save as '.h' in the project directory
  • in the project '.ino' file write the include definition for this files:

#include <mylib1.h> //< > file in the library folder
#include "myfile1.h" // " " file in the project folder

all .h files in the project directory are opened with the project file in other tabs.
include order prevent compilation order