Arduino Keypads 项目使用教程
1. 项目的目录结构及介绍
目录结构
arduino_keypads/
├── examples/
│ ├── Basic/
│ ├── MultipleKey/
│ ├── Password/
│ └── README.md
├── src/
│ ├── Keypad.cpp
│ └── Keypad.h
├── .gitignore
├── LICENSE
├── README.md
└── library.properties
目录介绍
- examples/: 包含多个示例项目,展示了如何使用 Keypad 库。
- Basic/: 基本示例,演示如何检测单个按键。
- MultipleKey/: 演示如何检测多个按键同时按下。
- Password/: 演示如何使用按键输入密码。
- src/: 包含 Keypad 库的核心源文件。
- Keypad.cpp: Keypad 库的实现文件。
- Keypad.h: Keypad 库的头文件。
- .gitignore: Git 忽略文件配置。
- LICENSE: 项目许可证。
- README.md: 项目说明文档。
- library.properties: Arduino 库属性文件,包含库的元数据。
2. 项目的启动文件介绍
启动文件
项目的启动文件通常位于 examples/
目录下。以 Basic/
示例为例,启动文件为 Basic.ino
。
启动文件内容
#include <Keypad.h>
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'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // 行引脚
byte colPins[COLS] = {5, 4, 3, 2}; // 列引脚
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup(){
Serial.begin(9600);
}
void loop(){
char key = keypad.getKey();
if (key){
Serial.println(key);
}
}
启动文件介绍
- #include <Keypad.h>: 引入 Keypad 库。
- const byte ROWS = 4;: 定义键盘的行数。
- const byte COLS = 4;: 定义键盘的列数。
- char keys[ROWS][COLS]: 定义键盘按键的映射。
- byte rowPins[ROWS]: 定义行引脚。
- byte colPins[COLS]: 定义列引脚。
- Keypad keypad: 创建 Keypad 对象。
- void setup(): 初始化串口通信。
- void loop(): 循环检测按键并输出到串口。
3. 项目的配置文件介绍
配置文件
项目的配置文件为 library.properties
。
配置文件内容
name=Keypad
version=3.1.1
author=Mark Stanley, Alexander Brevig
maintainer=Mark Stanley, Alexander Brevig
sentence=Allows you to connect a keypad to an Arduino and read key presses.
paragraph=This library allows you to connect a keypad to an Arduino and read key presses. It supports multiple keypresses and is based on the Keypad tutorial.
category=Device Control
url=https://github.com/Chris--A/Keypad
architectures=*
includes=Keypad.h
配置文件介绍
- name: 库的名称。
- version: 库的版本。
- author: 库的作者。
- maintainer: 库的维护者。
- sentence: 库的简短描述。
- paragraph: 库的详细描述。
- category: 库的分类。
- url: 库的 GitHub 地址。
- architectures: 支持的架构。