发一个4X4的键盘查询
const int numRows=4;
const int numCols=4;
const int debounceTime=20;//
const char keymap={//按键面板排列
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
const int rowPins={3,4,5,6};
const int colPins={7,8,9,10};
void setup()
{
Serial.begin(9600);
for (int row=0;row<numRows;row++)
{
pinMode(rowPins,INPUT);
digitalWrite(rowPins,HIGH);
}
for (int col=0;col<numCols;col++)
{
pinMode(colPins,OUTPUT);
digitalWrite(colPins,HIGH);
}
}
void loop()
{
char key=getkey();
if(key!='X')
{
Serial.print("Got key:");
Serial.println(key);
}
}
char getkey()
{
char key='X';
for (int col=0;col<numCols;col++)
{
digitalWrite(colPins,LOW);
for (int row=0;row<numRows;row++)
{
if(digitalRead(rowPins)==LOW)
{
delay(debounceTime);
while(digitalRead(rowPins)==LOW);
key=keymap;
}
}
digitalWrite(colPins,HIGH);
}
return key;
} 好分享~谢谢了。 本帖最后由 lqh 于 2016-5-2 12:07 编辑
做一个小试验Si?KWA51# Si?b做一个小试验Si?KWA51# 谢谢楼主无私奉献仿制了一个带I2C的1602程序
#include <Keypad.h>
//LingShun lab
#include <Wire.h>
#include <LiquidCrystal_I2C.h> //引用I2C库
//设置LCD1602设备地址,这里的地址是0x3F,一般是0x20,或者0x27,具体看模块手册
LiquidCrystal_I2C lcd(0x3F,16,2);
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup(){
lcd.init(); // 初始化LCD
lcd.backlight(); //设置LCD背景等亮
Serial.begin(9600);
}
void loop(){
char customKey = customKeypad.getKey();
if (customKey){
Serial.println(customKey);
lcd.setCursor(0,0); //设置显示指针
lcd.print(customKey); //输出字符到LCD1602上
lcd.setCursor(9,1);
lcd.print(" Z.W.F.");
}
}
页:
[1]