Hi
I have two questions:
- How to create global #define variable and call it in the class
- How to use LiquidCrystal.h between multiple classes. (Able to update LCD Display from the class)
Hi
I have two questions:
Define "global" and why you need this? You could put it in a .h that you include where you need it. You could also use template to pass that at instantiation time
Best would probably be to pass by reference the LCD object to the class as a parameter when you instantiates your object. This way you don't hardware in your class how the LCD needs to be wired.
- How to create global #define variable and call it in the class
A #define statement does NOT create a variable.
You can not CALL a variable. You can ONLY call functions.
So, the answer to your question is that you can't.
- How to use LiquidCrystal.h between multiple classes. (Able to update LCD Display from the class)
Just like you'd use digitalRead() from any class. Just do it.
Of course, what you probably meant was "How do I use an instance of LiquidCrystal in multiple classes?". The answer is that the sketch should be responsible for creating the instance, and pass, by reference, the instance to any instances of classes that need to use it.
If you don't understand what that means, you are not ready to be writing nested classes.
My question is simpler, because I am not very good at C++ programming...
In a class I created, I can use Serial.print("") but I cannot use lcd.print("").
Why and how to solve this?
Why and how to solve this?
The why is simple. You are doing something wrong. The how is a lot more complex. Since only you can see your code, only you can fix the problem.
As Paul said, it's difficult to say what is wrong without seeing your code.
One option of accessing lcd instance in multiple class is by passing it as parameter to some function.
For e.g. Say you have two class PrintHi, PrintHello
PrintHi hi
PrintHello hello
hi.init(lcd)
hello.init(lcd)
store lcd instance in the global scope of each class and all the functions inside that class can access lcd instance.
Shahreza:
2. How to use LiquidCrystal.h between multiple classes. (Able to update LCD Display from the class)
To update the LCD display from a class, you have just to declare : extern class LiquidCrystal lcd; in the .h file of your class. After, you use it as in the main file.
Michel_P:
In a class I created, I can use Serial.print("") but I cannot use lcd.print("").
What makes you think that you "cannot" use lcd.print("")? I don't understand "cannot" in this context. Do you have a phobia relating to the word 'lcd'? Is your keyboard missing the 'l' key? Are you getting a compiler error? Will your code not upload? Does it upload, but not do what you think it should? What is "cannot"?
you have just to declare : extern class LiquidCrystal lcd; in the .h file of your class. After, you use it as in the main file.
You do NOT include class as part of the extern statement.
PaulS:
You do NOT include class as part of the extern statement.
to clarify this statement... if your class is in a separate header file, you need a reference to where the class may find the lcd object. The (proper) way to do it in the header would be:
extern LiquidCrystal lcd;
as Paul pointed out: no class keyword.
BUT... you really do not want to do it like this if you are building a proper library. This creates a dependency on the object name in the main (.ino) file.
Any Class member function then depends on that external name. The programmer (even the author) must know (remember) to look to the header file and find the right name for his lcd object and make sure it is properly referenced/called in any member function .. yuck!
Suppose you had multiple LCD displays/objects? Ouch!
If you really want to do this properly and extensibly, you need to study friendship and inheritance.
Here be dragons.
If you really want to do this properly and extensibly, you need to study friendship and inheritance.
My guess, and it's only a guess, is that OP wants his/her class to create the instance of the LiquidCrystal class, but doesn't know how, since there are no no-argument constructors.
OP really needs to respond to this thread before we do any more guessing.
PaulS:
My guess, and it's only a guess, is that OP wants his/her class to create the instance of the LiquidCrystal class...
My (thusly qualified) guess is that he doesn't yet know that this is what he wants!
standing by...