Go Down

Topic: Multi file arduino sketch (Read 9731 times) previous topic - next topic

TedSpencer

Is it possible to break an Arduino sketch into several files (using something akin to an "Include" to point to the 'external' files? The point is to make it easier to navigate through, and edit, a large sketch.

lloyddean


TedSpencer

Ah. I thought there might be. No doubt I framed my question improperly: How does one include such a file in the 'main' sketch? What is the syntax of such an include? Where does one store the included file?

KeithRB

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

TedSpencer

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

PaulS

Quote
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.

TedSpencer

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.

PatoPato

The same just happened to me!
Thanks!!!

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

PeterH


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.
I only provide help via the forum - please do not contact me for private consultancy.

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.

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' ?.

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

Robin2


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.


Quote
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
Two or three hours spent thinking and reading documentation solves most programming problems.

electronicsguy

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?

sterretje

The IDE takes care of it at the moment that you include the .h file.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

electronicsguy

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?

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy