I am working on a very long code that requires multiple functions defined. I want to split the code into two files as main code ("Feeder_Control" in the screenshot) and functions ("connections" in the screenshot) How to import the second file into main code to use its functions?
PS: In C/C++ same thing is done with #include "connections.h". How is it done in Arduino IDE.
-
3Though the accepted answer works, I feel while the Arduino "IDE" is great for beginners but it doesn't quite scale well beyond a couple of files. I'd highly recommend checking out platformio as it provides much better project management via integration with atom and vscode, enables you to work with different frameworks including Arduino for AVR, STM32, ESP8266, ESP32 etc and supports debugging if you have the required hardware.Shreyas Murali– Shreyas Murali2019年01月15日 19:39:05 +00:00Commented Jan 15, 2019 at 19:39
-
Don't cross post. You already asked this on SO.gre_gor– gre_gor2019年01月16日 17:53:19 +00:00Commented Jan 16, 2019 at 17:53
2 Answers 2
There is no need to use an #include
directive for the additional .ino file.
Before compilation starts, the Arduino IDE concatenates all .ino files in your sketch into a single file, starting with the .ino file that matches the sketch folder name, followed by the rest of the .ino files in alphabetical order. So there is no difference between one massive .ino file and breaking the same code among multiple .ino files, other than the greater ease of navigating the code via tabs. After a bit more processing, this file is compiled as C++.
Reference: https://arduino.github.io/arduino-cli/latest/sketch-build-process/#pre-processing
Two comments to answer by per1234 (although old): You can have multiple .ino files in a project. However:
- You can not separate the preprocessor portion from the main file (The file with the project's name) which must include ALL preprocessing.
- Arduino IDE compiles all files in a project in a specific order (alphabetically by filename) and includes them into the main .ino file during the linking stage. Code in later .ino files can access variables and functions defined in earlier ones, but not vice versa.
-
Please link to the answer and/or state who posted it, rather than saying "answer#1" - the order of answers changes over time, and depending upon the type of ordering selected. Please edit and update your answer.Greenonline– Greenonline2024年03月22日 09:31:47 +00:00Commented Mar 22, 2024 at 9:31
-
the details you mention, are in the linked page. per1234 is the the tester for Arduino CLI at Arduino and main author of the Arduino CLI documentation including the linked page.2024年03月22日 09:45:58 +00:00Commented Mar 22, 2024 at 9:45
-
what is that about the preprocessor? there is no such limitation.2024年03月22日 09:47:35 +00:00Commented Mar 22, 2024 at 9:47
-
1>
Code in later .ino files can access variables and functions defined in earlier ones, but not vice versa
That's correct for variables, but not for functions: When concatenating all .ino files into a single .cpp, function prototypes from all .ino files are generated and added to the start of that .cpp file. If that works well, all functions can be used from any part of your code. And it's a good idea to collect all global variables in the projectname.ino file, which comes first in the .cpp concatenation.DataFiddler– DataFiddler2024年03月22日 22:20:56 +00:00Commented Mar 22, 2024 at 22:20 -
Thanks for the details. Yet, I have a main file and in it a large portion of building HTML (or CSS) output code. That HTML portion is currently located in the root of the main. I wish to move it out to a separate file, but it is not defined as an Arduino function and thus it has no prototype function and will not be linked. Can I define it as a function? What is the best way to proceed? Thxsamtal– samtal2024年03月23日 15:47:09 +00:00Commented Mar 23, 2024 at 15:47