arduino

Multiple tabs/files in Arduino


I am seeking a good tutorial that covers all of this. I need to break the project into multiple tabs/ino files, just to make it more clear.

So after you open a new tab, there are a few questions I would like to ask:

  1. If the main project file, let's say main, has another 2 tabs let's say A and B, so, every function in B would be visible to main and also A?

  2. What happens with interrupts? If I have some interrupt that I define in file A, can it call the function of the interrupt in the main file?

  3. What happens with defines? and includes? if file A including some library lets say Wire, does the main file also see it and vice versa?

What's the strategy to work with files? Do you add all your libraries to the main, or should you also add them to other files? (For example a file that deal with gyro and has to include some library).


Solution

  • I have always had trouble using the Arduino IDE for more than one source file. I would lean towards using something like Arduino-Makefile which gives you more control over the build process of your Arduino project.

    1. You need to create a header file that declares these functions and then include that header file in you .ino file.
    2. Assuming you are using the AVR library you can have the interrupt handler call a function (shown in Interrupt Handler). Then simply call the interrupt_handler() function outside of the ISR.
    3. You should include the dependencies that are need for each file. That way if A or Bremoves #include <Wire.h>, your main file will still include the dependency. The file will not be included twice because of header include guards.

    Interrupt Handler

    #include <avr/interrupt.h>
    #include "A.h"
    
    /* Declare our ISR */
    ISR(interrupt_vector)
    {
        /* Call our handler (located in A) */
        interrupt_handler();
    }