搜尋程式碼

如何使用4x4軟體鍵盤配合Arduino

如何使用4x4軟體鍵盤配合Arduino

喺呢個教學入面,我哋會探討點樣將一個4x4軟性鍵盤連接同使用喺Arduino度。呢個鍵盤有十六個掣,但係只需要八個引腳就可以連接,令到佢成為一個慳位又有效率嘅輸入裝置,適合用喺各種項目。跟住呢個指南去到最後,你就會學識點樣讀取鍵盤嘅輸入,同埋根據嗰啲輸入去觸發動作。

4x4 soft keypdad

我哋會用Keypad函式庫去同鍵盤介面連接,等我哋可以容易啲偵測到邊個掣被撳。我哋會實作嘅程式會將被撳嘅掣顯示喺序列監視器度,並且喺特定嘅掣被撳嘅時候採取行動,例如掣'4'。如果想睇多啲視覺上嘅指引,記得睇吓條片(喺片入面00:00嘅位置)。

硬件解說

呢個項目嘅主要組件係4x4軟性鍵盤,佢由16個按鈕組成,排列成一個網格。每個按鈕連接到Arduino上一個特定嘅引腳,令到佢可以偵測到按鈕幾時被撳。鍵盤用矩陣掃描嘅方法運作,行同列會依次序被啟動,去決定邊個掣被撳。

要連接鍵盤,你需要用Arduino上八個數碼引腳。行通常連接到引腳2至5,而列就連接到引腳6至9。呢個安排令到Arduino可以有效咁讀取每個掣嘅狀態。

數據表詳情

闊度69mm
長度76mm
線材長度84mm
連接器闊度20mm
連接器窿距2.56mm

  • 確保行同列引腳接線正確,避免讀錯。
  • 如有需要,用上拉電阻去穩定輸入讀數。
  • 喺軟件度做防彈跳處理,避免一次撳掣讀到多次。
  • 逐個掣測試,確保功能正常。
  • 保持鍵盤乾爽同乾淨,以維持效能。

接線指示

Arduino wiring for 4x4 soft keypad
Arduino wiring for 4x4 soft keypad

要將4x4鍵盤接線到Arduino,將鍵盤嘅行引腳連接到Arduino上嘅數碼引腳,方法如下:將第一行連接到引腳2,第二行連接到引腳3,第三行連接到引腳4,第四行連接到引腳5。至於列引腳,將第一列連接到引腳6,第二列連接到引腳7,第三列連接到引腳8,第四列連接到引腳9

呢個設定令到Arduino可以有效咁掃描行同列。記得再三檢查接線,確保每個引腳正確對應佢指定嘅行或列。喺條片入面,有簡單討論過其他接線配置(喺片入面01:15嘅位置)。如果你選擇用唔同嘅接線方法,只需要相應咁更新程式入面嘅引腳號碼就得。

程式碼示例同講解

喺程式碼入面,我哋首先引入Keypad函式庫,並且定義鍵盤嘅佈局。行同列用陣列嚟設定,其中rowPins對應連接到行嘅數碼引腳,而colPins就對應列。

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

喺呢段摘錄入面,我哋定義咗行同列嘅數量,以及掣嘅字元佈局。呢個設定對於根據被啟動嘅行同列去識別邊個掣被撳係好重要嘅。

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

setup函數初始化序列通訊,速率係9600波特。呢個對於將按鍵數據傳送到序列監視器做偵錯同驗證好重要。

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

loop函數入面,我哋持續用keypad.getKey()檢查有冇掣被撳。如果有掣被撳,佢就會將掣嘅值打印到序列監視器。另外,如果掣'4'被撳,佢就會打印一個特定訊息,表示掣'4'已經被啟動。程式碼嘅呢個部分容許根據用戶輸入做實時互動。

示範/預期效果

當所有嘢設定好,並且將程式碼上載到Arduino之後,你應該會喺序列監視器度見到被撳嘅掣顯示出嚟。如果你撳掣'4',訊息「Key 4 is pressed」就會出現,確認輸入成功被偵測到。要小心常見嘅陷阱,例如浮動輸入或者接線錯誤,呢啲可能會導致錯誤讀數(喺片入面02:30嘅位置)。

影片時間戳

  • 00:00 - 鍵盤介紹
  • 01:15 - 接線指示
  • 02:30 - 程式碼解說同示範

圖片

4x4 soft keypdad back view
4x4 soft keypdad back view
4x4 soft keypdad
4x4 soft keypdad
4x4 soft keypdad connector
4x4 soft keypdad connector
4x4 soft keypdad
4x4 soft keypdad
Arduino wiring for 4x4 soft keypad
Arduino wiring for 4x4 soft keypad
41-Resources for this code
語言: C++
/*
 * Original source: https://playground.arduino.cc/Code/Keypad
 * This is the Arduino code for a 4x4 keypad.
 * Watch the video for details and demo: http://youtu.be/qIDzzG2LUtg
 * 
 * Written by Ahmad Nejrabi for Robojax Video channel: www.Robojax.com
 * Date: December 18, 2017, 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
 * 
 */
/* @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 = 4; //four columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6,7,8,9}; //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();
    // 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'){
    Serial.println("Key 4 is pressed");
  }
}

你可能需要嘅嘢

資源與參考資料

暫時冇資源。

文件📁

Arduino 函式庫 (zip)