Here is the code I put together. I added the few lines that check for key events at the end of the Loop.
I don't guess there will be any issues with the two -- one displaying LCD info received over serial, and the other sending keypad input back on serial?
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
#include <LiquidCrystal.h>
#include <EEPROM.h>
// --- EEPROM ADDRESS DEFINITIONS
#define LCD_BACKLIGHT_ADDRESS 1 // EEPROM address for backlight setting
#define BAUD_ADDRESS 2 // EEPROM address for Baud rate setting
#define SPLASH_SCREEN_ADDRESS 3 // EEPROM address for splash screen on/off
#define ROWS_ADDRESS 4 // EEPROM address for number of rows
#define COLUMNS_ADDRESS 5 // EEPROM address for number of columns
// --- SPECIAL COMMAND DEFINITIONS
#define BACKLIGHT_COMMAND 128 // 0x80
#define SPECIAL_COMMAND 254 // 0xFE
#define BAUD_COMMAND 129 // 0x81
// --- ARDUINO PIN DEFINITIONS
uint8_t RSPin = 2;
uint8_t RWPin = 3;
uint8_t ENPin = 4;
uint8_t D4Pin = 5;
uint8_t D5Pin = 6;
uint8_t D6Pin = 7;
uint8_t D7Pin = 8;
uint8_t BLPin = 9;
char inKey; // Character received from serial input
uint8_t Cursor = 0; // Position of cursor, 0 is top left, (rows*columns)-1 is bottom right
uint8_t LCDOnOff = 1; // 0 if LCD is off
uint8_t blinky = 0; // Is 1 if blinky cursor is on
uint8_t underline = 0; // Is 1 if underline cursor is on
uint8_t splashScreenEnable = 1; // 1 means splash screen is enabled
uint8_t rows = 4; // Number rows, will be either 2 or 4
uint8_t columns = 20; // Number of columns, will be 16 or 20
uint8_t characters; // rows * columns
// initialize the LCD at pins defined above
LiquidCrystal lcd(RSPin, RWPin, ENPin, D4Pin, D5Pin, D6Pin, D7Pin);
/* ----------------------------------------------------------
In the setup() function, we'll read the previous baud,
screen size, backlight brightness, and splash screen state
from EEPROM. Serial will be started at the proper baud, the
LCD will be initialized, backlight turned on, and splash
screen displayed (or not) according to the EEPROM states.
----------------------------------------------------------*/
void setup() {
// initialize the serial communications:
setBaudRate(EEPROM.read(BAUD_ADDRESS));
// Read rows and columns from EEPROM
// Will default to 2x16, if not previously set
rows = EEPROM.read(ROWS_ADDRESS);
if (rows != 4)
rows = 2;
columns = EEPROM.read(COLUMNS_ADDRESS);
if (columns != 20)
columns = 16;
// set up the LCD's number of rows and columns:
lcd.begin(columns, rows);
// Set up the backlight
pinMode(BLPin, OUTPUT);
setBacklight(EEPROM.read(LCD_BACKLIGHT_ADDRESS));
// Do splashscreen if set
splashScreenEnable = EEPROM.read(SPLASH_SCREEN_ADDRESS);
if (splashScreenEnable != 0)
{
if (columns == 16)
{
lcd.print("www.SparkFun.com");
lcd.setCursor(0, 1);
lcd.print(" Serial LCD Kit ");
delay(2000);
lcd.clear();
}
else
{
lcd.setCursor(0, 1);
lcd.print(" www.SparkFun.com ");
lcd.setCursor(0, 2);
lcd.print(" Serial LCD Kit ");
delay(2000);
lcd.clear();
}
}
}
void loop() {
while (Serial.available() > 0) {
inKey = Serial.read();
// Check for special LCD command
if ((inKey & 0xFF) == SPECIAL_COMMAND)
SpecialCommands();
// Backlight control
else if ((inKey & 0xFF) == BACKLIGHT_COMMAND) {
// Wait for the next character
while (Serial.available() == 0)
;
setBacklight(Serial.read());
}
// baud rate control
else if ((inKey & 0xFF) == BAUD_COMMAND) {
// Wait for the next character
while (Serial.available() == 0)
;
setBaudRate(Serial.read());
}
// backspace
else if (inKey == 8)
{
Cursor--;
LCDDisplay(0x20);
Cursor--;
}
// horizontal tab
else if (inKey == 9)
Cursor += 5;
// line feed
else if (inKey == 10)
Cursor += columns - Cursor % columns;
// carriage return
else if (inKey == 13)
Cursor += columns;
// finally (since no special commad received), just display the received character
else
LCDDisplay(inKey);
}
//Check for key events
char key = keypad.getKey();
if (key) {
Serial.println(key);
}
}
/* ----------------------------------------------------------
SpecialCommands() is reached if SPECIAL_COMMAND is received.
This function will wait for another character from the serial
input and then perform the desired command. If a command is
not recognized, nothing further happens and we jump back into
loop().
----------------------------------------------------------*/
void SpecialCommands()
{
// Wait for the next character
while (Serial.available() == 0)
;
inKey = Serial.read();
// Clear Display
if (inKey == 1)
{
Cursor = 0;
lcd.clear();
}
// Move cursor right one
else if (inKey == 20)
Cursor++;
// Move cursor left one
else if (inKey == 16)
Cursor--;
// Scroll right
else if (inKey == 28)
lcd.scrollDisplayRight();
// Scroll left
else if (inKey == 24)
lcd.scrollDisplayLeft();
// Turn display on
else if ((inKey == 12) && (LCDOnOff == 0))
{
LCDOnOff = 1;
lcd.display();
}
// Turn display off
else if (inKey == 8)
{
LCDOnOff = 0;
lcd.noDisplay();
}
// Underline Cursor on
else if (inKey == 14)
{
underline = 1;
blinky = 0;
lcd.noBlink();
lcd.cursor();
}
// Underline Cursor off
else if ((inKey == 12) && (underline == 1))
{
underline = 0;
lcd.noCursor();
}
// Blinking box cursor on
else if (inKey == 13)
{
lcd.noCursor();
lcd.blink();
blinky = 1;
underline = 0;
}
// Blinking box cursor off
else if ((inKey == 12) && (blinky = 1))
{
blinky = 0;
lcd.noBlink();
}
// Set Cursor position
else if ((inKey & 0xFF) == 128)
{
// Wait for the next character
while (Serial.available() == 0)
;
inKey = Serial.read();
Cursor = inKey;
}
else if (inKey == 30)
{
if (splashScreenEnable)
splashScreenEnable = 0;
else
splashScreenEnable = 1;
EEPROM.write(SPLASH_SCREEN_ADDRESS, splashScreenEnable);
}
else if (inKey == 3)
{
// 20 columns
columns = 20;
EEPROM.write(COLUMNS_ADDRESS, columns);
lcd.begin(columns, rows);
Cursor = 0;
}
else if (inKey == 4)
{
// 16 columns
columns = 16;
EEPROM.write(COLUMNS_ADDRESS, columns);
lcd.begin(columns, rows);
Cursor = 0;
}
else if (inKey == 5)
{
// 4 lines
rows = 4;
EEPROM.write(ROWS_ADDRESS, rows);
lcd.begin(columns, rows);
Cursor = 0;
}
else if (inKey == 6)
{
// 2 lines
rows = 2;
EEPROM.write(ROWS_ADDRESS, rows);
lcd.begin(columns, rows);
Cursor = 0;
}
}
void LCDDisplay(char character)
{
int currentRow = 0;
characters = rows * columns;
// If Cursor is beyond screen size, get it right
while (Cursor >= characters)
Cursor -= characters;
while (Cursor < 0)
Cursor += characters;
if (Cursor >= columns)
currentRow = Cursor / columns;
lcd.setCursor(Cursor % columns, currentRow);
lcd.write(character);
Cursor++;
}
void setBacklight(uint8_t backlightSetting)
{
analogWrite(BLPin, backlightSetting);
EEPROM.write(LCD_BACKLIGHT_ADDRESS, backlightSetting);
}
void setBaudRate(uint8_t baudSetting)
{
// If EEPROM is unwritten (0xFF), set it to 9600 by default
if (baudSetting == 255)
baudSetting = 4;
switch (baudSetting)
{
case 0:
Serial.begin(300);
break;
case 1:
Serial.begin(1200);
break;
case 2:
Serial.begin(2400);
break;
case 3:
Serial.begin(4800);
break;
case 4:
Serial.begin(9600);
break;
case 5:
Serial.begin(14400);
break;
case 6:
Serial.begin(19200);
break;
case 7:
Serial.begin(28800);
break;
case 8:
Serial.begin(38400);
break;
case 9:
Serial.begin(57600);
break;
case 10:
Serial.begin(115200);
break;
}
if ((baudSetting >= 0) && (baudSetting <= 10))
EEPROM.write(BAUD_ADDRESS, baudSetting);
}