【Arduino基礎(chǔ)教程】4*4矩陣鍵盤(pán)

4*4 Keypad

01準(zhǔn)備材料

  • Arduino Uno 開(kāi)發(fā)板沮焕;
  • 4*4矩陣鍵盤(pán)
  • 8根跳線

02接線

  • Keypad Pin R1 –> Arduino Pin 2
  • Keypad Pin R2 –> Arduino Pin 3
  • Keypad Pin R3 –> Arduino Pin 4
  • Keypad Pin R4 –> Arduino Pin 5
  • Keypad Pin C1 –> Arduino Pin 6
  • Keypad Pin C2 –> Arduino Pin 7
  • Keypad Pin C3 –> Arduino Pin 8
  • Keypad Pin C4 –> Arduino Pin 9

03所需庫(kù)文件

  • 在Arduino IDE 1.6.2 或者以上版本中, 項(xiàng)目->加載庫(kù)->管理庫(kù)中搜索Keypad,然后安裝即可拉宗。

  • 也可以在這里下載庫(kù)遇汞,然后手動(dòng)添加到IDE中。


04初始化設(shè)置

keypad(makeKeymap(userKeymap), row[], col[], rows, cols)

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 );

05常用函數(shù)

void begin(makeKeymap(userKeymap))

Initializes the internal keymap to be equal to userKeymap
[See File -> Examples -> Keypad -> Examples -> CustomKeypad]

char waitForKey()

This function will wait forever until someone presses a key. Warning: It blocks all other code until a key is pressed. That means no blinking LED's, no LCD screen updates, no nothing with the exception of interrupt routines.

char getKey()

Returns the key that is pressed, if any. This function is non-blocking.

KeyState getState()

Returns the current state of any of the keys.
The four states are IDLE, PRESSED, RELEASED and HOLD.

boolean keyStateChanged()

New in version 2.0: Let's you know when the key has changed from one state to another. For example, instead of just testing for a valid key you can test for when a key was pressed.

setHoldTime(unsigned int time)

Set the amount of milliseconds the user will have to hold a button until the HOLD state is triggered.

setDebounceTime(unsigned int time)

Set the amount of milliseconds the keypad will wait until it accepts a new keypress/keyEvent. This is the "time delay" debounce method.

addEventListener(keypadEvent)

Trigger an event if the keypad is used. You can load an example in the Arduino IDE.
[See File -> Examples -> Keypad -> Examples -> EventSerialKeypad] or see the KeypadEvent Example code.


06示例代碼

#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 );

void setup(){
  Serial.begin(9600);
}

void loop(){
  char key = keypad.getKey();

  if (key != NO_KEY){
    Serial.println(key);
  }
}


07參考文獻(xiàn)

http://playground.arduino.cc//Code/Keypad

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市空入,隨后出現(xiàn)的幾起案子络它,更是在濱河造成了極大的恐慌,老刑警劉巖歪赢,帶你破解...
    沈念sama閱讀 216,744評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件化戳,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡埋凯,警方通過(guò)查閱死者的電腦和手機(jī)点楼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)白对,“玉大人掠廓,你說(shuō)我怎么就攤上這事∷δ眨” “怎么了蟀瞧?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,105評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)条摸。 經(jīng)常有香客問(wèn)我悦污,道長(zhǎng),這世上最難降的妖魔是什么钉蒲? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,242評(píng)論 1 292
  • 正文 為了忘掉前任切端,我火速辦了婚禮,結(jié)果婚禮上顷啼,老公的妹妹穿的比我還像新娘踏枣。我一直安慰自己,他們只是感情好钙蒙,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,269評(píng)論 6 389
  • 文/花漫 我一把揭開(kāi)白布椰于。 她就那樣靜靜地躺著,像睡著了一般仪搔。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蜻牢,一...
    開(kāi)封第一講書(shū)人閱讀 51,215評(píng)論 1 299
  • 那天烤咧,我揣著相機(jī)與錄音,去河邊找鬼抢呆。 笑死煮嫌,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的抱虐。 我是一名探鬼主播昌阿,決...
    沈念sama閱讀 40,096評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了懦冰?” 一聲冷哼從身側(cè)響起灶轰,我...
    開(kāi)封第一講書(shū)人閱讀 38,939評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎刷钢,沒(méi)想到半個(gè)月后笋颤,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體廓俭,經(jīng)...
    沈念sama閱讀 45,354評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡亿胸,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,573評(píng)論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了账劲。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片阱缓。...
    茶點(diǎn)故事閱讀 39,745評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡非凌,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出荆针,到底是詐尸還是另有隱情敞嗡,我是刑警寧澤,帶...
    沈念sama閱讀 35,448評(píng)論 5 344
  • 正文 年R本政府宣布祭犯,位于F島的核電站秸妥,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏沃粗。R本人自食惡果不足惜粥惧,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,048評(píng)論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望最盅。 院中可真熱鬧突雪,春花似錦、人聲如沸涡贱。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,683評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)问词。三九已至督函,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間激挪,已是汗流浹背辰狡。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,838評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留垄分,地道東北人宛篇。 一個(gè)月前我還...
    沈念sama閱讀 47,776評(píng)論 2 369
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像薄湿,于是被迫代替她去往敵國(guó)和親叫倍。 傳聞我的和親對(duì)象是個(gè)殘疾皇子偷卧,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,652評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容