查看: 2134|回复: 3
|
让Arduino读取键盘
[复制链接]
|
|
这里提供两种方式让Arduino读取键盘,矩阵(Matrix)键盘和PS2键盘。要Arduino侦察到键盘,唯有安装键盘程序库(library)。
矩阵键盘
要让Arduino侦察到矩阵键盘,就必须使用Arduino Playground里的Keypad 键盘库,此 键盘库支持3x4, 4x4以及各种矩阵结构的键盘。
安装Keypad键盘库
下载Arduino Keypad键盘库
把keypad.zip里的文件夹解压至Arduino软件的libraries文件夹,如图
完成键盘库安装后,可以连接Arduino与键盘来测试了
4x4矩阵keypad示范
我用的4x4矩阵键盘,如图
键盘和Arduino接法如下
Arduino
| 4x4 Keypad
| D2
| 1
| D3
| 2
| D4
| 3
| D5
| 4
| D6
| 5
| D7
| 6
| D8
| 7
| D9
| 8
|
上载以下代码至Arduino- #include <Keypad.h>
- const byte ROWS = 4; // Four rows
- const byte COLS = 4; // Four columns
- //Define the keymap
- char keys[ROWS][COLS] = {
- {'1','2','3','A'},
- {'4','5','6','B'},
- {'7','8','9','C'},
- {'*','0','#','D'}
- };
- //// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
- byte rowPins[ROWS] = {2,3,4,5};
- // Connect keypad COL0, COL1, COL2 and COL3 to these Arduino pins.
- byte colPins[COLS] = {6,7,8,9}; //connect to column pinouts
- // Create 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);
- }
- }
复制代码 完成上载后,用Arduino软件的Serial Monitor来测试(Baud rate必须设成9600)。
当测试时,只要按下谋键,其返回值将显示在Serial Monitor。
本帖最后由 西门庆33 于 16-4-2013 04:57 PM 编辑
|
|
|
|
|
|
|
|
楼主 |
发表于 16-4-2013 04:46 PM
|
显示全部楼层
PS2键盘
要让Arduino侦察到PS2键盘,就必须使用PS2keypad 键盘库
安装Arduino PS2键盘库
- 下载Arduino PS2键盘库
- 将下载了的文件(PS2keyboard.zip)解压至Arduino软件的libraries文件夹
- 安装方法与安装keypad相似,请参考之
PS2键盘示范
根据下面接线连接键盘至Arduino
Keyboard
| Arduino
| 4 (+5V)
| 5V
| 3 (GND)
| GND | 5 (Clock)
| Digital Pin 3
| 1 (Datak)
| Digital Pin 4
|
上载以下代码至Arduino- #include <PS2Keyboard.h>
- const int DataPin = 8;
- const int IRQpin = 5;
- PS2Keyboard keyboard;
- void setup() {
- delay(1000);
- keyboard.begin(DataPin, IRQpin);
- Serial.begin(9600);
- Serial.println("Keyboard Test:");
- }
- void loop() {
- if (keyboard.available()) {
- char c = keyboard.read(); // read the next key
- // check for some of the special keys
- if (c == PS2_ENTER) {
- Serial.println();
- } else if (c == PS2_TAB) {
- Serial.print("[Tab]");
- } else if (c == PS2_ESC) {
- Serial.print("[ESC]");
- } else if (c == PS2_PAGEDOWN) {
- Serial.print("[PgDn]");
- } else if (c == PS2_PAGEUP) {
- Serial.print("[PgUp]");
- } else if (c == PS2_LEFTARROW) {
- Serial.print("[Left]");
- } else if (c == PS2_RIGHTARROW) {
- Serial.print("[Right]");
- } else if (c == PS2_UPARROW) {
- Serial.print("[Up]");
- } else if (c == PS2_DOWNARROW) {
- Serial.print("[Down]");
- } else if (c == PS2_DELETE) {
- Serial.print("[Del]");
- } else {
- Serial.print(c); // otherwise, just print all normal characters
- }
- }
- }
复制代码 完成上载后,用Arduino软件的Serial Monitor来测试(Baud rate必须设成9600)。
当测试时,只要按下谋键,其返回值将显示在Serial Monitor。
本帖最后由 西门庆33 于 16-4-2013 05:05 PM 编辑
|
|
|
|
|
|
|
|
楼主 |
发表于 16-4-2013 11:04 PM
|
显示全部楼层
捕充一下PS2插座
之前用这个PS2插座,是从坏了的主机板拆下来的,焊接很麻烦。
之后买了这条一出二(一公二母)的PS2延长线,把中间切断,变成了两个PS2插孔,用在两个projects
本帖最后由 西门庆33 于 16-4-2013 11:06 PM 编辑
|
|
|
|
|
|
|
|
楼主 |
发表于 26-4-2013 11:48 AM
|
显示全部楼层
增加按键音效
具体浏览这里- #include <Keypad.h>
- const byte ROWS = 4; // Four rows
- const byte COLS = 4; // Four columns
-
- //Define the keymap
- char keys[ROWS][COLS] = {
- {'1','2','3','A'},
- {'4','5','6','B'},
- {'7','8','9','C'},
- {'*','0','#','D'}
- };
-
- //// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
- byte rowPins[ROWS] = {6,7,8,9};
-
- // Connect keypad COL0, COL1, COL2 and COL3 to these Arduino pins.
- byte colPins[COLS] = {2,3,4,5}; //connect to column pinouts
-
- // Create 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){
- delay(50); //act as debounce
- beep();
- Serial.println(key);
- }
- }
-
- #define SPEAKER_PIN 10
- void beep(){
- tone(SPEAKER_PIN,2000,90);
- delay(20);
- noTone(SPEAKER_PIN);
- }
复制代码 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|