Include H and Cpp file in main sketch

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.

Thanks in advanced

Smart_Agriculture_Project.ino (2.3 KB)
IotLibraries.h (1.3 KB)
functions.cpp (770 Bytes)

Here is the error I am getting. Maybe I am doing something very wrong.
Context – share whatever you see with others in seconds (

Your post was MOVED to it's current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

If you get errors when compiling please copy them from the IDE using the "Copy error messages" button and paste the clipboard here in code tags

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

Think there is an option in IDE to add h and cpp files to project (needs IDE restart?) just make sure you #include <Arduino.h> in those

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

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

there are a couple issues

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

there may be other

There is a confusion of terms here

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

Surely, only closing and re-opening that particular sketch is required.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.