14

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.

look here.

2
  • 3
    Though 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. Jan 15, 2019 at 19:39
  • Don't cross post. You already asked this on SO.
    – gre_gor
    Jan 16, 2019 at 17:53

2 Answers 2

22

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

0

Two comments to answer by per1234 (although old): You can have multiple .ino files in a project. However:

  1. You can not separate the preprocessor portion from the main file (The file with the project's name) which must include ALL preprocessing.
  2. 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.
6
  • 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. Mar 22 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.
    – Juraj
    Mar 22 at 9:45
  • what is that about the preprocessor? there is no such limitation.
    – Juraj
    Mar 22 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. Mar 22 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? Thx
    – samtal
    Mar 23 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.