由於工作關係,隔了兩個多星期才又開始進行實驗,這次實驗的主題是從電料行買的3x4 Keypad,這個 Keypad 總共有八隻接腳,由於不知道行與列的線路連接方式,於是將拆開後背後的螺絲拆開看看線路圖:
仔細檢視每一個行(自右而左,分別為COL0 ~ COL2)與列(自上而下)的線路,知道接腳(從左至右)如下:
如要使用喇叭,Arduino有一個涵式庫已經定義好音調,只要用Include將"pitches.h"包含進來:
以本單元的Do, Re ~ Si,總共七個,定義名稱如下:
在畫Fritizing的電路圖時,內建沒有KeyPad圖形,可從http://code.google.com/p/fritzing/ 搜尋Keypad後下載Keypad.fzz匯入即可。雖然跟我買的Keypad圖樣不同,但是鍵盤排列位置是一樣的。
數字7為Si的音,其他鍵盤則為高音Do。
仔細檢視每一個行(自右而左,分別為COL0 ~ COL2)與列(自上而下)的線路,知道接腳(從左至右)如下:
| 資料型態 | 位元 | 值範圍 |
|---|---|---|
| 位置 | KeyPad接腳 | Arduino接腳 |
| COL-0 | 5 | 9 |
| COL-1 | 6 | 8 |
| COL-2 | 7 | 7 |
| ROW-0 | 1 | 13 |
| ROW-1 | 2 | 12 |
| ROW-2 | 3 | 11 |
| ROW-3 | 4 | 10 |
如要使用喇叭,Arduino有一個涵式庫已經定義好音調,只要用Include將"pitches.h"包含進來:
以本單元的Do, Re ~ Si,總共七個,定義名稱如下:
#define NOTE_C5 523 // Do #define NOTE_D5 587 // Re #define NOTE_E5 659 // Me #define NOTE_F5 698 // Fa #define NOTE_G5 784 // So #define NOTE_A5 880 // La #define NOTE_B5 988 // Si
[材料]
- 麵包板 x 1
- Arduino 主板 x 1
- 3x4 Keypad x 1
- 喇叭 x 8
- 單心線 x 8
[接線]
將Keypad的第一隻腳接Pin 13,第二隻腳接Pin12,餘此類推至第八隻腳接Pin6,將喇叭的兩條線,一條接GND,一條接Pin4。在畫Fritizing的電路圖時,內建沒有KeyPad圖形,可從http://code.google.com/p/fritzing/ 搜尋Keypad後下載Keypad.fzz匯入即可。雖然跟我買的Keypad圖樣不同,但是鍵盤排列位置是一樣的。
[程式]
// 引用 Keypad library
#include <Keypad.h>
#include <pitches.h>
// 3x4 Keypad
const byte ROWS = 4; // 4 Rows
const byte COLS = 3; // 3 Columns
int duration = 500; // 500 miliseconds
// 定義 Keypad 的按鍵
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
// 定義 Keypad 連到 Arduino 的接腳
byte rowPins[ROWS] = {13, 12, 11, 10}; // 連到 Keypad 的 4 個 Rows: Row0, Row1, Row2, Row3
byte colPins[COLS] = {9, 8, 7}; // 連到 Keypad 的 3 個 Columns: Column0, Column1, Column2
// 建立 Keypad 物件
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
Serial.begin(9600);
}
void loop(){
// 讀取 Keypad 的輸入
char key = keypad.getKey();
// NO_KEY 代表沒有按鍵被按下
if (key != NO_KEY){ // 假如有按鍵被按下,就印出按鍵對應的字元
switch (key) {
case '1':
tone(4, NOTE_C5, duration);
break;
case '2':
tone(4, NOTE_D5, duration);
break;
case '3':
tone(4, NOTE_E5, duration);
break;
case '4':
tone(4, NOTE_F5, duration);
break;
case '5':
tone(4, NOTE_G5, duration);
break;
case '6':
tone(4, NOTE_A5, duration);
break;
case '7':
tone(4, NOTE_B5, duration);
break;
default:
tone(4, NOTE_C8, duration);
}
Serial.println(key);
}
}
[執行結果]
當按下數字1時,會發出Do的音,按下數字2時,會發出Re的音...直到數字7為Si的音,其他鍵盤則為高音Do。

鍵盤我找不到呢@@
回覆刪除然後有4*4的鍵盤嗎?
3x4鍵盤在一般電子材料行都有賣, 可以找看看!! 有4x4鍵盤, 右邊多一排ABCD四個鍵!!
刪除3*4為什麼會有8隻腳?是有共同腳?
回覆刪除張貼留言