raschemmel,
Its not really the same thing. The single lcd object you are using in your loop is an automatic and
has to be intialized each time. This does all kinds of things, from initializing the object
to clearing the display and will be very time consuming because of the call to
lcd.begin(rows, cols) (which is missing in your example) that must be in the loop.
If you look at the i2clcdguesser sketch it does this. But in it's case there is only
a single lcd so it re-uses the same object.
In the more general case of supporting multiple LCDs, you really want to have
multiple objects and have a seperate LiquidCrystal object for each physical LCD module.
So the better way is to declare an array of LiquidCrystal objects, then initialized them in setup().
Then simply use them by referencing them.
i.e.
// create lcd objects.
#include <LiquidCrystal.h>
LiquidCrystal lcd[] = {
LiquidCrystal(12, 9, 5, 4, 3, 2),
LiquidCrystal(12, 10, 5, 4, 3, 2),
LiquidCrystal(12, 11, 5, 4, 3, 2),
LiquidCrystal(12, 12, 5, 4, 3, 2)
};
#if 0
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd[] = {
LiquidCrystal_I2C(0x27,2,1,0,4,5,6,7,3,NEGATIVE),
LiquidCrystal_I2C(0x38,6,5,4,0,1,2,3,7,NEGATIVE),
};
#endif
struct lcdGeometry
{
uint8_t rows;
uint8_t cols;
};
// geometry for each display
const lcdGeometry lcdGeom[] = {
{ 2, 16},
{ 2, 16},
{ 2, 16},
{ 2, 16}
};
void setup()
{
const int numLCDs = sizeof(sizeof(lcd)/sizeof(lcd[0]));
for(int i = 0; i < numLCDs; i++)
{
lcd[i].begin(lcdGeom[i].cols, lcdGeom[i].rows);
}
for(int i = 0; i< numLCDs; i++)
{
lcd[i].print("Hello World");
lcd[i].setCursor(0, 1);
lcd[i].print("Display #");
lcd[i].print(i);
}
}
void loop(){}
You can use any lcd object you want in the list.
(4bit, SR, i2c etc...)
The restriction of using an array is that all the objects have to be the same type.
4bit or i2c types etc...
If you use seperate objects not in array, then the types can be mixed and matched as desired.
For supporting multiple LCDs using the same MCP23017, all you would need
is a library that allows you to specify the pins in the constructor.
Maybe something that uses pin numbers 0-15 where 8-15 are portB pins.
If you have that, you could simply create an array like the i2c example above.
But in the larger sense driving multiple LCDs from a single i/o expander chip
seems a bit odd. Normally, with multiple displays
you would simply use a MCP23008 or PCF8574 on each display.
Although when using multiple displays on a single i/o expander,
you could actually write to more than one at the same time by simply
strobing E on all the displays you want to write to.
--- bill