#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
//#include <Wire.h>
LiquidCrystal_I2C lcd(0x27,16,2); //設定LCD位置0x27,設定LCD大小為16*2
//#include <LiquidCrystal.h>
//定義4*4 KeyBoard每一按鍵名稱
char key[4][4]={
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte colPin[4]={5,4,3,2};//按鍵Col分別接到PIN5 PIN4 PIN3 PIN2
byte rowPin[4]={9,8,7,6};//按鍵Row分別接到PIN9 PIN8 PIN7 PIN6
//使用Keypad函式庫
Keypad keypad = Keypad(makeKeymap(key), rowPin, colPin, 4, 4);
//
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
//const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
///const int rs = 12, en = 11, d4 = A5, d5 = A4, d6 = A3, d7 = A2;
///LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int x,y;
void setup() {
Serial.begin(9600);//鮑率設為9600
//
lcd.init(); //初始化LCD
lcd.backlight(); //開啟背光
lcd.print("Hi!");
// set up the LCD's number of columns and rows:
///lcd.begin(16, 2);
// Print a message to the LCD.
//lcd.print("hello, world!");
x=0,y=0;
}
void loop() {
// put your main code here, to run repeatedly:
char key = keypad.getKey();//讀取所按按鍵名稱
//key不為空值
if(key!=NO_KEY){
Serial.println(key);//顯示所按按鍵名稱
//lcd.setCursor(0, 1);
lcd.setCursor(x++, y);
lcd.print(key);
if(x>15) {
x=0;
y++;
if(y>1) y=0;
}
}
//
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
//lcd.setCursor(0, 1);
// print the number of seconds since reset:
//lcd.print(millis() / 1000);
}