How can my .cpp file read my .ino file variables?

I am splitting my project into multiple files to make it a little more manageable. I have done one class (.h & .cpp files) that works fine so far and it is used by the main .ino file. The variables in the class can also be accessed by the main .ino file.

I have just created my second class (.h & .cpp files) and I have realised that I need to get a variables value from the main .ino file. How do I do that?

You should pass the value from the sketch to the class via a function.

I have just created my second class (.h & .cpp files) and I have realised that I need to get a variables value from the main .ino file.

that's usually a sign of a design flaw in your class architecture / OOP concepts if it's dependant on the existence and access to an external variable

if you can't really design around it, you could have a begin() method for your class where you pass the variable (by value, pointer, reference depending on the need)

add
extern float myFloat;
etc
to you C++ file.
and then just access myFloat from within your class functionts.
Not very OO but then neither is Arduino's globals

Castingflame:
I have just created my second class (.h & .cpp files) and I have realised that I need to get a variables value from the main .ino file. How do I do that?

You could pass the value in to one of the class's functions as an argument. You could also provide the class with a reference to the variable. If the class doesn't need to modify the variable, make it a 'const' reference.

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