Multiple source files in an Arduino project

The Arduino IDE does an amazing job of hiding all the technical details of what it’s doing. This allows the some of the easiest creation of programs I have seen since the days of BASIC. From the looks of many of the forum questions over at the main Arduino website, it seems there are certainly a bunch of new programmers doing just this.

As an experience embedded programmer, much of what the Arduino IDE does seems to be magic to me. How can I just type a function without a prototype? How does it know what libraries to include when I don’t specify them? How is this able to work at all?

Over the past week, I have learned a bit more about what is going on behind the scenes. Apparently, the IDE has a preprocessor that converts the Arduino “Sketch” in to C++ code, generating prototypes and such automatically. I have already ran in to one problem with this.

Many other things remain a mystery, but at least one more has been explained today. I was very curious how one could split up a larger project in to multiple files. As it turns out, the Arduino IDE makes this super simple… Just make a new tab in the existing project.

In the Arduino 1.0.4 (current released version) editor, I noticed a square icon with a down arrow in it on the right side of the window, under the magnifying glass “search” button. I had seen this before, with “New Tab” and other options in it. I had assumed this was so you could switch between multiple projects in the same window, but now I understand this is how you have multiple files in the same project. Just create a tab, and put your code in it.

So, if I have my setup() and loop() in the main tab, and create a second tab with doSomething(), I can then call doSomething() from setup() or loop(). More magic.

I will be splitting up my various code snippets in to separate files for easy including in future projects.

I post this because I expect maybe I am not the only “experienced embedded programmer” who doesn’t read the manual.

7 thoughts on “Multiple source files in an Arduino project

      1. Lewis Balentine

        Affirmative:
        Look for the file “foo.cpp” in your build directory where “foo” is the name of your main INO project file. You will need to do this while the IDE is still open so that it does not delete the files. You can specify the build directory in your preferences file.

        For more information see the section “Setting up the Arduino IDE:” in my PDF document “Arduino Nano Project One.pdf” at http://www.keywild.com/arduino

        Reply
        1. Allen Huffman Post author

          I definitely need to check that out. I love the concept of open source, but documentation is often severely lacking when it comes to the details. I’ve already had to dig in to the Ethernet source code to try to figure out why it wasn’t working properly, and really didn’t want to have to do that to better understand the IDE ;)

          Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.