Learn how to create and manage objects in Arduino C+ + , particularly when dealing with foreign classes, like LiquidCrystal_I2C for custom LCD displays.
---
This video is based on the question https://stackoverflow.com/q/63796449/ asked by the user 'Sebastian' ( https://stackoverflow.com/u/3143878/ ) and on the answer https://stackoverflow.com/a/63796781/ provided by the user 'rhaport' ( https://stackoverflow.com/u/8932168/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Arduino C+ + : Create object from foreign class
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Creating an Object from a Foreign Class in Arduino C+ + : A Guide
When working with Arduino, you may find the need to extend the functionalities of libraries or create custom classes to manage specific hardware features, such as an LCD display. In this guide, we will explore a common issue that developers face while creating an object from a foreign class in Arduino C+ + and provide a clear, structured solution to help you navigate through it. Let's dive in!
Understanding the Problem
You have a basic functioning LCD display connected to your Arduino Mega 2560. The code you used shows a simple message, "Hello", on the screen. However, you want to expand the functionality of your LCD display by creating a custom class, Lcd, to manage various operations related to the display.
The Current Setup
Here's a brief overview of the code structure you started with:
LCD Initialization:
[[See Video to Reveal this Text or Code Snippet]]
Basic functions (setup and loop):
[[See Video to Reveal this Text or Code Snippet]]
Transitioning to a Custom Class
In your transition to a custom class, you defined Lcd in separate header and source files. Here you planned to encapsulate the functionality of the LCD display. However, you encountered a problem where the display did not show any output despite the code compiling without errors.
The Core Issue
The Diagnosis: The main issue arises from the fact that you created a local instance of LiquidCrystal_I2C in the begin() method that shadows the class member named myLCD. Therefore, the member object remains uninitialized when the write() method is called. This could be why no output appears on the LCD after you attempt to display text.
The Solution
Step 1: Refactor the Constructor
To resolve this issue, we need to modify the constructor of the Lcd class. Instead of relying on default values, we should pass the necessary arguments (address, columns, and rows) as parameters to the constructor.
Here's how you can do that:
[[See Video to Reveal this Text or Code Snippet]]
Step 2: Update the begin() Method
You should also change the begin() method to remove the local instance creation of myLCD. Instead, you'll rely on the class member that was already initialized through the constructor.
Here's the revised begin() method:
[[See Video to Reveal this Text or Code Snippet]]
Step 3: Adjust the Main File
Finally, you’ll need to adjust how you instantiate your Lcd object in your main Arduino file to pass the required constructor arguments:
[[See Video to Reveal this Text or Code Snippet]]
Conclusion
By following the outlined steps, you should now be able to successfully create an object from the LiquidCrystal_I2C class within your custom Lcd class in Arduino C+ + . This method allows you to encapsulate additional functionalities while maintaining clean and efficient code. With this understanding, you're now better equipped to manage and utilize LCD displays in your Arduino projects.
Happy Coding!
If you have further queries or need assistance with your code, feel free to leave a comment below. We’d love to help!
Информация по комментариям в разработке