【Arduino基础教程】4*4矩阵键盘

简介: 4*4 Keypad01准备材料Arduino Uno 开发板;4*4矩阵键盘8根跳线02接线Keypad Pin R1 –> Arduino Pin 2Keypad Pin R2 –> Arduino Pin 3...
4*4 Keypad

01准备材料

  • Arduino Uno 开发板;
  • 4*4矩阵键盘
  • 8根跳线

02接线

  • Keypad Pin R1 –> Arduino Pin 2
  • Keypad Pin R2 –> Arduino Pin 3
  • Keypad Pin R3 –> Arduino Pin 4
  • Keypad Pin R4 –> Arduino Pin 5
  • Keypad Pin C1 –> Arduino Pin 6
  • Keypad Pin C2 –> Arduino Pin 7
  • Keypad Pin C3 –> Arduino Pin 8
  • Keypad Pin C4 –> Arduino Pin 9

03所需库文件

  • 在Arduino IDE 1.6.2 或者以上版本中, 项目->加载库->管理库中搜索Keypad,然后安装即可。

  • 也可以在这里下载库,然后手动添加到IDE中。


04初始化设置

keypad(makeKeymap(userKeymap), row[], col[], rows, cols)

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] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[cols] = {8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );

05常用函数

void begin(makeKeymap(userKeymap))

Initializes the internal keymap to be equal to userKeymap
[See File -> Examples -> Keypad -> Examples -> CustomKeypad]

char waitForKey()

This function will wait forever until someone presses a key. Warning: It blocks all other code until a key is pressed. That means no blinking LED's, no LCD screen updates, no nothing with the exception of interrupt routines.

char getKey()

Returns the key that is pressed, if any. This function is non-blocking.

KeyState getState()

Returns the current state of any of the keys.
The four states are IDLE, PRESSED, RELEASED and HOLD.

boolean keyStateChanged()

New in version 2.0: Let's you know when the key has changed from one state to another. For example, instead of just testing for a valid key you can test for when a key was pressed.

setHoldTime(unsigned int time)

Set the amount of milliseconds the user will have to hold a button until the HOLD state is triggered.

setDebounceTime(unsigned int time)

Set the amount of milliseconds the keypad will wait until it accepts a new keypress/keyEvent. This is the "time delay" debounce method.

addEventListener(keypadEvent)

Trigger an event if the keypad is used. You can load an example in the Arduino IDE.
[See File -> Examples -> Keypad -> Examples -> EventSerialKeypad] or see the KeypadEvent Example code.


06示例代码

#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] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //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();

  if (key != NO_KEY){
    Serial.println(key);
  }
}


07参考文献

http://playground.arduino.cc//Code/Keypad

目录
相关文章
|
Android开发
解决 Android Studio 日志乱码
解决 Android Studio 日志乱码
2020 0
解决 Android Studio 日志乱码
|
3月前
|
缓存 监控 搜索推荐
电商生态协同的关键:API接口在电商数据对接中的应用与实践
电商数据对接API接口是连接电商平台与外部系统的智慧桥梁,通过标准化协议实现商品管理、订单处理、支付结算、物流追踪及数据分析等全链路支持。本文从核心功能、对接流程、应用场景和优化策略四个方面解析其技术逻辑与实践路径。API接口助力店铺管理自动化、精准营销与跨境电商全链路管理,同时通过安全防护、性能调优与合规管理提升效能,推动电商行业向智能化、高效化发展。
|
4月前
|
数据可视化 Rust 机器学习/深度学习
mlop.ai 无脑使用教程 (机器学习工具 WandB/ClearML 的首个国区开源平替)
mlop.ai 是首个为国区用户优化的机器学习工具,全栈免费开源,是主流付费解决方案 ClearML/WandB 的开源平替。常规实验追踪的工具经常大幅人为降速,mlop因为底层为Rust代码,能轻松支持高频数据写入。如需更多开发者帮助或企业支持,敬请联系cn@mlop.ai
247 12
mlop.ai 无脑使用教程 (机器学习工具 WandB/ClearML 的首个国区开源平替)
|
7月前
|
机器学习/深度学习 人工智能 自然语言处理
VideoGrain:零样本多粒度视频编辑神器,用AI完成换装改场景,精准控制每一帧!
VideoGrain 是悉尼科技大学和浙江大学推出的零样本多粒度视频编辑框架,基于调节时空交叉注意力和自注意力机制,实现类别级、实例级和部件级的精细视频修改,保持时间一致性,显著优于现有方法。
207 0
VideoGrain:零样本多粒度视频编辑神器,用AI完成换装改场景,精准控制每一帧!
|
存储 Unix 程序员
非常详细!操作系统基础【文件系统实现】
非常详细!操作系统基础【文件系统实现】
|
存储 监控 Java
线上OOM排查
本文介绍了JDK工具的使用方法及其应用场景。首先详细说明了`jps`、`jstack`、`jstat`和`jmap`等工具的基本用法及参数含义,帮助开发者实时监控Java进程的状态,诊断线程问题及内存使用情况。接着介绍了`jvisualvm.exe`和`MemoryAnalyzer.exe`两款内存诊断工具,展示了如何通过这些工具进行内存分析。最后,文章提供了在线上OOM故障排查的具体步骤,并给出了解决方案示例,以便开发者更好地理解和解决实际问题。
311 2
线上OOM排查
|
安全 虚拟化 Windows
|
存储 前端开发 Java
第十一章 Spring Cloud Alibaba nacos配置中心
第十一章 Spring Cloud Alibaba nacos配置中心
321 0
|
机器学习/深度学习 达摩院 监控
今日开源:阿里达摩院最新框架FederatedScope来了!让联邦学习从可用到好用
今日开源:阿里达摩院最新框架FederatedScope来了!让联邦学习从可用到好用
722 0
|
存储 XML JSON
高效的网络传输协议设计protobuf
高效的网络传输协议设计protobuf
451 1