17

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 Answers2

24

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

per1234
  • 4,278
  • 2
  • 24
  • 43
2

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.
samtal
  • 37
  • 2