Hello i need you help to understand how Arduino IDE is working.
I have main sketch .ino file, and I want to make a header file with the declaration and a .cpp file with the functions that I use.
I want to do that to keep the main .ino file a simple as possible.
I have encounter many error so far and fix a few but they still coming.
If someone can see my code and show me the correct way to do it I would be grateful.
They may be a lot of syntax error because I haven't compile yet so don't mind those.
The easiest way to keep your main sketch simple (???) would be to just rename your functions.cpp file to be functions.ino and put all the info in your .h file into functions.ino
With that extension name, the IDE will combine all of them together when it does to compilation and you won't have to worry about any header files
You can add .h and .cpp files to a project as tabs in the IDE as long as you use the #include "myLibrary.h" style of #include so that the sketch looks in the sketch directory for the library files. The IDE does not need a restart if you do this, nor indeed does it if you install a library using the Library Manager
@UKHeliBob,
Are you sure about your statement? My experience is that if I add .h or .cpp using the IDE (new tab command), I always get an empty file. If I need to add an existing .h and .cpp, I need to put it in the .ino directory and re-open the sketch.
Can you tell me in detail how you add the existing files to the IDE using the
IDE?
in general, the .h file for filename.cpp should be called filename.h. in your case, it should be functions.h
and i usually include that .h in the corresponding .c/.cpp file as well as one or more other .c/.cpp files.
a common problem is any definition (instead of declaration) in the .h file will be multiple defined. in your case, all your variables are defined (as well as initialized in the .h. they should be defined in the .cpp file and declared (e.g. extern int sensorPin) in the .h
another issue is the #endif on line 3 for the #ifndef should be the last line of the file.
and if RawToLux() is only used in your funcitons.cpp, there's no need for it to be declared in the .h
i'm not sure the 2 includes are needed in the .h. typically they are only needed in the .h if the provide information for the symbols in the .h
You can add new .h and .cpp files to a sketch by adding new tabs. that is what I was describing. They will, of course be empty. If you simply copy the files to the sketch directory with the IDE running then it will remain blissfully unaware of them until it is restarted
If you want to add an existing .h or .cpp (or both) to a sketch you can do this using Sketch/Add File in the IDE if you want local copies. New tabs will be created for each file.
If you want to add a library from the sketchbook/libraries folder to the sketch then you use Sketch/Include Library
If you want to install a zipped library and #include it in the sketch you use Sketch/Include Library/Add .Zip library
None of these 3 methods require the IDE to be restarted, at least with a reasonably recent version of the IDE