How to use external files

Hello everybody!

Im trying very hard to build a overly complicated bike-controller in order to teach myself programming.

Ive now at a stage where my very messy code is becoming a serious problem to debug, so ive figured its time to learn how to write a library, to externalize some of the code functions, where they can each be troubleshooted separately.

Now, since im very self-taught, i have some severe gaps in my programming knowledge, so im hoping for some forum guidance here, simply put; how to structuralize a "big project" code.

So could anyone please explain in a way so i can understand, what to put into the different files (.h/.cpp), and how that code must differ from whats used in a sketch?
Im currently very confused about how to reference between the different files, or if the referencing is all done in the "main" sketch (.ino)..

To setup some concrete examples;
I have a function to read my buttons, that is to readGPIO from a number of mcp23008 pin extenders, sort out the output pins and trunkate the rest into a binary state byte array (10101110 for example, if button "A" is on, its 1, else 0).

I then have another function to check the pressed-duration, to sort out long or short clicks, with a similar array containing each buttons last button-press-type as a value 0 (unpressed), 1 (pressed), 2 (shortclicked), or 3 ( longclicked).

I am then using a separate function to keep a record of the last button press type and the last button read time, so i dont have to keep the i2c bus busy with constant read requests.
Meaning, if i would poll a button state from my main loop once each loop, it would reply last state without polling for a new one between each button-check interval.

The idea is to have the code respond to each button change by resetting the last button-press-type to 0, to only respond to each new buttonpress once.

So how would i put thease functions into a library, or a external .h/.cpp file and keep the function usage "as is" in the rest of the code?

..I have NO clue about constructors and such "fancy" words, more than i know i should know them before calling myself a "programmer"... :slight_smile:

I would not go to the trouble of creating a library if the code is only to be used in one project. The value of a library is when you want to use the same code in several projects.

A simple way to organize your code is to create additional .ino files in the same folder as your principal project .ino file. All of the .ino files will appear as tabs in the Arduino IDE and the IDE will combine them all into a single file before compiling your code.

You can alternatively put your code into .h and/or ,cpp files and use #include "myfile.h" to include the file if it is in the same folder as the principal .ino file.

"Proper" C/C++ programmers use the .h files to define things and the .cpp files to contain the code but, as far as I can see, the compiler is quite happy when I put all my code into a .h file (by which you will realize that I do not consider myself a proper C programmer :slight_smile: )

Planning and Implementing a Program

I agree with Robin2 that for what you describe there is no benefit to writing a library. However, it's certainly useful when you do want to share code between sketches so if you still want to learn how to write libraries I recommend following this tutorial:

After doing that if you are left with any specific questions you can come back to the forum for help.

Ive studied those links provided and they helped me sort out some of my issues, but im still not getting how to link the files together, without defining stuff multiple times..

i have a couple of structures containing my modifiable data, witch i want to modify from different functions.

Now, i moved my structures to a "struct.h" file, and split up my functions to their respecive header files (like "inputs.h", "outputs.h", "display.h" and so on.

But since i now have to write to "struct.h" from multiple separate header files, i now have to include the "struct.h" into each header file, else i get compile errors within that file.

But when i try to compile the main project, i get "this and that was was re-defined" and so on.

So im still not getting how to link the different files together and how to access data across files.

Could anyone care to enlighten me?

Could anyone care to enlighten me?

It would be much easier to help if you post a shortened example that illustrates the problem.

Have you got #include "struct.h" at the top of every file in which you want to be able to refer to the struct?


Do you have include guards in your .h files? That's the:

#ifndef Morse_h
#define Morse_h

// the #include statement and code go here...


part of the tutorial I posted above. Of course you should modify the macro name according to each file name (e.g. struct_h).