I am confronted by an interesting problem. I would like to use an LCD and a 4x4 keypad in a project. I require quite a few pins for the rest of the project so I decided to try hooking up the LCD and the keypad to an MCP23017 I/O Expander Chip. I use the LiquidTWI2 library to control the LCD and Keypad_MC17 library to control the keypad. The serial output shows that the keypad is functioning as expected. Furthermore, the LCD lights up and a start up message displays based on code in the setup() section of my code. Here is the code:
#include <Keypad_MC17.h>
#include <Wire.h>
#include <Keypad.h>
#include <LiquidTWI2.h>
#define I2CADDR 0x20
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {3, 2, 1, 0}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad
Keypad_MC17 keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS, I2CADDR );
LiquidTWI2 lcd(0);
void setup() {
Serial.begin(9600);
keypad.begin();
lcd.setMCPType(LTI_TYPE_MCP23017);
lcd.begin(20,4);
lcd.print("Testing...");
Serial.print("Testing...");
}
void loop() {
char key = keypad.getKey();
if (key) {
changeDisplay(key);
}
}
void changeDisplay(char k) {
Serial.println(k);
lcd.clear();
lcd.print(k);
}
The problem is that the key presses do not show up on the LCD - as a matter of fact, nothing that I do to change the LCD display in the loop() does anything. I have played around with delays and separating the display changes into separate functions (as you can see in the above code) to no avail. Finally I removed the getKey() command and simply used a lcd.print(millis()) command to simulate a "Hello, World!" example and it finally worked.
So obviously, the problem lies with the getKey() command. It somehow locks out the MCP23017 into only accepting input from the B register to which the keypad is connected. Does anybody have experience with this library? Is there a way to get around this "lock out" to access the A pins?
I have not attached images of my wiring, because there seems to be no problem there. If you need it, however, just let me know and I'll upload some images.
Thanks in advance.
Casper