Trouble using own libraries and importing them

Hi all,

First post here, so if it is not in the right standard please dont get mad but tell me what to change and i’ll change is asap!

So my problem:

i have a couple of libraries i created and want to import in my main sketch. Lets call them lib1, lib2 and lib3.

lib1 and lib2 are classes with functionality for a valve and a motor.

in lib3 i want to create a “motorcontroller”, which needs to instantiate an object of lib 1 and 2.

This looks as follows:

class outputmanager {
  public:
    // constructor:
    outputmanager();


    // public attributes:
    int  ID       = 0;
    valve Valve;
    motor VirtualMotor; // virtualmotor

}

Hi all,

First post here, so if it is not in the right standard please dont get mad but tell me what to change and i'll change is asap!

So my problem:

i have a couple of libraries i created and want to import in my main sketch. Lets call them lib1, lib2 and lib3.

lib1 and lib2 are classes with functionality for a valve and a motor.

in lib3 i want to create a "motorcontroller", which needs to instantiate an object of lib 1 and 2.

This looks as follows:

class outputmanager {
  public:
    // constructor:
    outputmanager();


    // public attributes:
    valve Valve;
    motor VirtualMotor; // virtualmotor

}

This is all working fine but now comes my problem.

i started with 1 big library where all 3 classes were defined. Now i want to separate the classes using the .h and .cpp files.

i did this my creating the following files:

valve.h
valve.cpp

motor.h.
motor.cpp

controller.h
controller.cpp

Now i am having trouble with using the #include properly.

In the controller lib i import both valve and motor.h
in the cpp files i include the corresponding .h file.

In the main sketch i import all 3 libraries.

i have given all the libraries the guard:

#ifndef xxx
#define xxx

#endif

Now i am getting confused if this is the correct way to use the #include and all, but for now my code still compiles.

Now i want to create another separate file called Settings (.h/.cpp?). This file is no library but is just a file where some specific settings are defined and declared and where some enums and structs are created.
I need this file to be accessable by my main sketch AND all libraries.

This is where the errors start coming:

If i #include the Settings ONLY in my main sketch i get the error (Settings include is before class include):

var x is not defined in lib 1,2 and 3.

So i added the #include to the libs and then i get the error:

multiple definitions of var x... first declaration in main sketch.

I have provided the Settings file with #ifndef #define #endif. but it looks like it is just ignoring this statement.

So after this long post, i'm basically looking for the following answers:

  1. If using libraries in your main sketch and in another library, is including them in ALL and using #ifndef to prevent multiple definitions the right way of doing so? if not, what is?

  2. If you want to have a separate file for settings, which is NOT a class, do you need to name it a .h file, a .cpp file or something else (.ino for example)?

another weird thing (for me) is that when i remove the .cpp file and place that code on the bottom of te header file, the above error diappears. so my last question:

  1. When do you use .h, .cpp and other extensions?

Kind regards and thanks in advance,

jan

The lib1-3 work fine. Then when you use Settings.h then the problem starts. You just want to declare all the configuration in the system, and not anything else. So why don't you declare every variable in the main sketch and the libs? It would be a lot simpler. Just declare them in the sketch and libs.

Another way, if you are running low on flash memory. Note- this works only when the settings are constant and don't change.
You can use EEPROM for storing the settings values. Just upload one sketch to store the values on the EEPROM once, and then use your sketch. When you need the settings in the sketch, the you should use Eeprom.read() function.

Hope it helped you.

..Arnav

Hey,

Thank you for your reply.

i have thought about leaving the settings in the mail sketch, the person working with this later will need to change the settings every once in a while.
If he can change them in a separate file, i am making sure he is not messing around in the code and possibly destroying the machine :smiley:

If it is too much trouble to get this done myself, i might end up doing it like that anyways.

Yes, and your question is?

#include <lib1.h>
#include <lib2.h>

Duplicate of this thread

Duplicate topics merged

Why did you post the question twice ?

I was writing the post and pressed post instead of preview accidentally so thats prolly why it got posted twice. I did delete the shorter version afterwards but apperantly it's still here...