搜尋程式碼

使用4x3键盘配合Arduino

使用4x3键盘配合Arduino

喺呢個教學入面,我哋會探討點樣用一塊4x3鍵盤配合Arduino嚟讀取按鍵。呢塊鍵盤可以用喺好多唔同嘅項目度,例如整一個簡單嘅輸入介面畀保安系統,或者任何需要用戶輸入嘅應用。喺呢個指南完結之前,你會學識點樣讀取按鍵,同埋對特定嘅掣作出反應。

記得睇埋附帶嘅影片,入面有接線同程式碼嘅實際示範(影片00:00位置)。

硬件講解

呢個項目嘅主要組件包括4x3鍵盤同Arduino板。鍵盤由一個按鈕矩陣組成,排列成4行3列。當一個掣被撳嘅時候,佢會將特定嘅行連接到特定嘅列,令到Arduino可以辨認出邊個掣被撳咗。

Arduino板係中央控制器,負責讀取鍵盤輸入。佢用咗Keypad函式庫,呢個函式庫簡化咗管理矩陣鍵盤嘅過程,令偵測按鍵更加容易。

數據表詳情

製造商多間廠商
零件編號4x3鍵盤
邏輯電壓5 V
工作電流≤ 20 mA
按鍵壽命≥ 1,000,000次
封裝矩陣鍵盤

  • 正確咁將鍵盤嘅引腳連接到Arduino嘅數位引腳。
  • 視乎你嘅設定,如有需要就使用上拉或下拉電阻。
  • 確保Keypad函式庫已經加入咗你嘅Arduino IDE。
  • 喺軟件度做按鍵去抖,避免重複讀取。
  • 測試每個掣,確認接線同功能正常。

接線指示

Arduino_wiring_4x3_keypad

要將4x3鍵盤連接到Arduino,你需要將鍵盤嘅行同列引腳連接到Arduino嘅數位引腳。將行引腳(通常4個)連接到Arduino嘅引腳2345。列引腳(通常3個)就應該連接到引腳678

確保將鍵盤嘅接地引腳連接到Arduino嘅接地,而電源引腳就連接到5V輸出。呢個設定會令Arduino可以喺撳掣嘅時候讀取每個掣嘅狀態,用Keypad函式庫嚟處理矩陣掃描。

程式碼範例同講解

以下程式碼初始化鍵盤,並將撳到嘅掣輸出到序列監視器。按鍵值儲存喺一個字元陣列入面。

const byte ROWS = 4; //四行
const byte COLS = 3; //三列
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};

keys陣列定義咗鍵盤嘅佈局,對應返實體按鍵嘅排列。每個字元代表鍵盤上嘅一個掣。

跟住,我哋喺setup()函式入面設定鍵盤:

void setup(){
  Serial.begin(9600);
  Serial.println("Robojax 4x3 keypad");
}

呢段程式碼初始化序列通訊,鮑率係9600,等我哋可以喺序列監視器度睇到按鍵。

最後,主循環會檢查按鍵並作出相應反應:

void loop(){
  char key = keypad.getKey();
  if (key){
    Serial.println(key);
  } 
  if (key =='4'){
    Serial.println("Key 4 is pressed");
  } 
}

呢度,程式用keypad.getKey()檢查有冇掣被撳。如果有掣被撳,佢會印出嗰個掣嘅值。另外,如果撳到'4'呢個掣,就會顯示一個特定訊息。

示範/預期效果

當設定完成之後,撳鍵盤上任何一個掣,都應該會將對應嘅字元輸出到序列監視器。如果撳到'4'呢個掣,就會印出一個特別訊息,表示嗰個掣被啟動咗。呢個功能令你可以喺呢個基礎上建立更加複雜嘅應用。

要小心浮動輸入,因為佢可能會導致唔穩定嘅行為。確保所有連接都穩固,並測試每個掣嚟確認功能正常(影片00:00位置)。

圖片

4x3_kaypad-1
4x3_kaypad-1
4x3_kaypad-2
4x3_kaypad-2
4x3_kaypad-3
4x3_kaypad-3
4x3_kaypad-4
4x3_kaypad-4
4x3_kaypad-5
4x3_kaypad-5
Arduino_wiring_4x3_keypad
Arduino_wiring_4x3_keypad
128-Ardunino code for 4x3 keypad
語言: C++
/*
 * Library taken from: https://playground.arduino.cc/Code/Keypad
 * This is the Arduino code for a 4x3 keypad.
 * Watch the video for details and demo: http://youtu.be/qIDzzG2LUtg
resources page: www.Robojax.com/RJT104
 * 
 * Modified by Ahmad Shamshiri for the Robojax Video channel: www.Robojax.com/RJT104
 * Date: July 11, 2018 at 19:56 in Ajax, Ontario, Canada
 * Permission granted to share this code, given that this
 * note is kept with the code.
 * Disclaimer: This code is "AS IS" and for educational purposes only.
 * This code has been downloaded from https://robojax.com
 * Watch video instructions for this code: 
 * 
 */
/* @file HelloKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates the simplest use of the matrix Keypad library.
|| #
*/
#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] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6,7,8}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
  // Robojax 4x3 keypad test
  Serial.begin(9600);
  Serial.println("Robojax 4x3 keypad");
}
  
void loop(){
  // Robojax 4x3 keypad test
  char key = keypad.getKey();
    // Just print the pressed key
   if (key){
    Serial.println(key);
  } 
   // This checks if 4 is pressed, then do something. Here we print the text, but you can control something.
  if (key =='4'){
    // If key matches what you are looking for
    Serial.println("Key 4 is pressed");
  } 

}

你可能需要嘅嘢

資源與參考資料

暫時冇資源。

文件📁

Fritzing 文件

其他文件