Question about using objects in headers AND main

Hello there, you have a happy day!

I have a problem that it's bothering me for quite some time.
I have a MEGA and a 3.2" Touch Screen and I've made a program some months ago when I first started programming.
The original program was a mess and I'm re-writing it, as I want to make it more "clean".

I'm creating some header files that contains code that was in the original program, while including 2 libraries I've downloaded (UTFT and URTouch for the Touch Screen). The problem is, that I must create 2 objects in one of the header files (the objects are created in the .cpp file) and I must also use the same objects in the .ino file as well.

The logical problem of course, is that the 2 objects are defined twice and VS obviously has an error of multiple definitions of the same object. Anyone else using the same libraries and has some tips?

Will the LCD work if I create 4 different objects? See the attached code to see what I mean.
Thank you for your time and have a happy day :slight_smile:

Graphics.h (789 Bytes)

Graphics.cpp (5.17 KB)

Serena_Test.ino (863 Bytes)

Don't include the .cpp file in your sketch! Just include the .h.

Thank you for your answer John.

That's what I originally did, but then I can't use the objects I created in the .cpp file.
Should I create new ones in the .ino file?

The .cpp file implements an object. If that object contains other objects it would be a violation of the principle or encapsulation to let any code outside the object touch those objects directly.

You should add a method to your object to do whatever you think the main program should be doing to those contained objects. For example, instead of calling methods like myTouch.dataAvailable(), myTouch.read(), and myTouch.getX() in loop() you should call Serena_Graphics.update() and let THAT call the myTouch methods.

The proper way to encapsulate those two objects is to make them static members of the object class. Static members are variables that are shared by all instances of the object. Declaring and initializing those objects can be tricky but Google can help.

If there can only ever be one instance of Serena_Graphics then you should make the class a 'singleton'. It's a way of defining an object so that only one instance can be created. A Google search should come up with examples of singletons.

By Arduino convention, the setup method in your library should be called 'begin()' rather than 'Initialize_Screen()'.

Thank you so much for the time you took to help me out. I will try to follow the method you said. I hope I'll get some good results out of this.

I'll reply back later with my results.
Have a nice day.