Apologies if this is a stupid question but I'm fairly new to Arduino etc.
I'm trying to break down my code in parts to make it easier to manage.
The code will be build for Wemos D1 but I guess the question is more generic.
I understand that .h and .cpp files are use for libraries that you would include in your .ino file and then create an object accordingly to definitions etc.
I was wondering if you can make use of .h and/or .cpp files just to keep certain functions together without the need to create an object.
For example lets say I want to create math functions to manipulate 5 variables adding(a,b,c,d,e,), multi(a,b,c,d,e), strand(a,b,c,d,e) etc... All these functions use internally +,-,^ etc , so nothing special at the end. But I just want to have all these math functions together in a file. (Maybe I should not use .h and .cpp files for this and are there other ways to do this...)
Thanks for any help!
.h and .cpp files do not have to contain object oriented code. They can contain normal functions and variables
An alternative method is put more than one .ino file in the sketch directory. The IDE will combine them together before compiling the code
ah! So if I understand correctly, then I can have multiple .ino files in the sketch directory and they will all be compiled to a single code? Does order of functions matter when they are defined in the order of the code?
The order of function definitions in the code does not matter except in the case of functions with optional parameters which must be defined before they are called. Alternatively a function prototype can be put in the code before they are called even if the function is actually defined later
Many thanks for your reply! This is very helpful.
Don't forget that .h library files can be #included in code so you only need one master copy whereas you cannot do that with multiple .ino files
not sure what this is saying.
first, i don't believe (i've tried) that any .h files are necessary when there are multiple .ino files in the sketch directory because all the code is combined into a single file along with prototypes for functions and compiled.
outside of the Arduino IDE, files are individually compiled and .h files are used to provide information (prototype declarations) to the compiler when referencing symbols (functions and variables) defined in a separate file.
and yes, outside of the Arduino IDE, a symbol must be declared before it is used and is commonly done when a function is defined later in a file but referenced before it is defined.
the Arduino IDE allows shortcuts that are not explained in standard C/C++ descriptions
it seems that there are limitation if any additional files are .ino instead of .cpp. .cpp files require "#include "Arduino.h"
I have a detail tutorial step by step on how to separate out code from your sketch
How to Write Your Own Arduino Libraries
with example sketches
I don't believe that I said or implied that .h files were needed when multiple .ino files were used. To be clear, they are not required
i was just trying to point out how the Arduino IDE handles multiple .ino and additional .cpp files differently and that this impacts what is required of .h files
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.