Arduino - Keypad 键盘

Arduino - Keypad

Arduino - Keypad

The keypad is widely used in many devices such as door lock, ATM, calculator…
键盘广泛应用于门锁、ATM、计算器等多种设备中。

In this tutorial, we will learn:
在本教程中,我们将学习:

  • How to use keypad 3x4 and keypad 4x4 with Arduino.
    如何将键盘 3x4 和键盘 4x4 与 Arduino 一起使用。
  • How to read value from keypad 3x4 and keypad 4x4 with Arduino.
    如何使用 Arduino 从键盘 3x4 和键盘 4x4 读取值。
  • How to verify the password inputted from keypad
    如何验证从键盘输入的密码

About Keypad 关于键盘

Keypad

The keypad is a set of buttons arranged in rows and columns (called matrix). Each button is called key
键盘是一组按行和列排列的按钮(称为矩阵)。每个按钮都称为键

Keypad has various types. Two popular types for DIY projects are keypad 3x4 (12 keys) and keypad 4x4 (16 keys).
键盘有多种类型。DIY 项目的两种流行类型是键盘 3x4(12 键)和键盘 4x4(16 键)。

Pinout 引脚排列

Keypad pins are divided into two groups: row and column.
键盘引脚分为两组:行和列。

Keypad 3x4 has 7 pins: 4 row-pins (R1, R2, R3, R4) and 3 column-pin (C1, C2, C3).
键盘 3x4 有 7 个引脚:4 个排引脚(R1、R2、R3、R4)和 3 列引脚(C1、C2、C3)。

Keypad 4x4 has 8 pins: 4 row-pins (R1, R2, R3, R4) and 4 column-pin (C1, C2, C3, C4).
键盘 4x4 有 8 个引脚:4 个排引脚(R1、R2、R3、R4)和 4 列引脚(C1、C2、C3、C4)。

Keypad Pinout

How It Works 它是如何工作的

This section is the in-depth knowledge. DON’T worry if you don’t understand. Ignore this section if it overloads you, and come back in another day. Keep reading the next sections.
本节是深入的知识。如果您不明白,请不要担心。如果它使您超负荷,请忽略此部分,并在另一天再回来。继续阅读下一节。

The process of detecting the key pressing is called scanning keypad.
检测按键的过程称为扫描键盘。

It is called “scanning” because it checks one key by one key.
它被称为“扫描”,因为它逐个键检查一个键。

Row-pins are connected to Arduino’s output pins
Row-pin连接到Arduino的输出引脚

Column pins are connected to Arduino’s input pins (INPUT_PULLUP, in this state, the value of the input pin is HIGH if the key is not pressed).
列引脚连接到Arduino的输入引脚(INPUT_PULLUP,在此状态下,如果未按下该键,则输入引脚的值为HIGH)。

For each row: 对于每一行:

  • Sets all row-pins is HIGH.
    将所有行引脚设置为高电平。
  • Sets only the current row-pin to LOW.
    仅将当前行引脚设置为低电平。
  • Reads the state of each column.
    读取每列的状态。
    • If a column-pin is HIGH ⇒ key at (row, column) is NOT pressed.
      如果列引脚为高电平,则不按下 (行、列) 处⇒键。
    • If a column-pin is LOW ⇒ key at (row, column) is pressed.
      如果列引脚为低电平,则按下 (行、列) 处⇒键。
  • Repeats the above process for the next row-pins.
    对下一个行引脚重复上述过程。

※ NOTE THAT: ※ 注意事项:

The above is one of the methods to scan keypad. We can invert all HIGH to LOW and all LOW to HIGH to scan keypad.
以上是扫描键盘的方法之一。我们可以将所有高电平反转为低电平,将所有低电平反转为高电平以扫描键盘。

Why does keypad is arranged and connected as a matrix? This makes the scanning process complicated. Why do not use each key as an independent button, then the state of the key is simply determined by reading the state of a button?
为什么键盘是以矩阵的形式排列和连接的?这使得扫描过程变得复杂。为什么不把每个键都当成一个独立的按钮,那么键的状态就是通过读取一个按钮的状态来确定的呢?

⇒ As we know, an independent button requires one Arduino’s pin and GND. Let’s take keypad 4x4 as an example. If we each key as an independent button, it requires 16 Arduino pin for 16 keys plus GND pin. If we arranged a connected key in matrix form, we just need to use 8 Arduino’s pin, so we can save Arduino’s pin. In short, the answer is: to save the Arduino pins.
⇒ 众所周知,一个独立的按钮需要一个Arduino的引脚和GND。让我们以键盘 4x4 为例。如果我们每个按键作为一个独立的按钮,它需要 16 个 Arduino 引脚用于 16 个按键加上 GND 引脚。如果我们以矩阵形式排列一个连接的密钥,我们只需要使用 8 个 Arduino 的引脚,这样我们就可以保存 Arduino 的引脚。简而言之,答案是:保存Arduino引脚。

Wiring Diagram 接线图

Arduino Keypad Wiring Diagram

This image is created using Fritzing. Click to enlarge image
此图像是使用 Fritzing 创建的。点击放大图片

How To Program For Keypad 如何为键盘编程

Thanks to Keypad library, using keypad with Arduino is a piece of cake, no matter whether you understand how the keypad works or not.
多亏了键盘库,无论您是否了解键盘的工作原理,使用带有 Arduino 的键盘都是小菜一碟。

Arduino Code Arduino代码

Keypad 3x4 键盘 3x4

#include <Keypad.h>const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 3; //three columnschar keys[ROW_NUM][COLUMN_NUM] = {{'1','2','3'},{'4','5','6'},{'7','8','9'},{'*','0','#'}
};byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3}; //connect to the column pinouts of the keypadKeypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );void setup(){Serial.begin(9600);
}void loop(){char key = keypad.getKey();if (key){Serial.println(key);}
}

Keypad 4x4 键盘 4x4

#include <Keypad.h>const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columnschar keys[ROW_NUM][COLUMN_NUM] = {{'1','2','3', 'A'},{'4','5','6', 'B'},{'7','8','9', 'C'},{'*','0','#', 'D'}
};byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypadKeypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );void setup(){Serial.begin(9600);
}void loop(){char key = keypad.getKey();if (key){Serial.println(key);}
}

Quick Steps 快速步骤

  • Navigate to the Libraries icon on the left bar of the Arduino IDE.
    导航到 Arduino IDE 左侧栏上的 Libraries 图标。
  • Search “keypad”, then find the keypad library by Mark Stanley, Alexander Brevig
    搜索“键盘”,然后找到 Mark Stanley、Alexander Brevig 的键盘库
  • Click Install button to install keypad library.
    单击“安装”按钮安装键盘库。

Arduino keypad library

  • Copy the above code and open with Arduino IDE
    复制上面的代码并使用Arduino IDE打开
  • Click Upload button on Arduino IDE to upload code to Arduino
    单击Arduino IDE上的“上传”按钮,将代码上传到Arduino
  • Open Serial Monitor 开放式串行监视器
  • Press some keys on keypad
    按键盘上的一些键
  • See the result in Serial Monitor
    在串行监视器中查看结果

Keypad and Password 键盘和密码

A popular application of keypad is the password input. In this application, we specify two special keys:
键盘的一个流行应用是密码输入。在此应用程序中,我们指定了两个特殊键:

  • A key to start/re-start the password input. For example, key ""
    用于启动/重新启动密码输入的密钥。例如,键“
  • A key to terminate the password input. For example, key “#”
    用于终止密码输入的键。例如,键“#”

The password will be a string that contains the remaining keys, except for two selected special keys.
密码将是一个字符串,其中包含其余密钥,但两个选定的特殊密钥除外。

When a key is pressed.
按下某个键时。

  • If the key is NOT neither "" nor “#”, append the key to the user’s input password string.
    如果密钥既不是“
    ”也不是“#”,则将密钥追加到用户的输入密码字符串中。
  • If the key is “#”, compare the user’s input password string with the password to determine the input password is correct or not, and then clear the user’s input password string
    如果密钥为“#”,则将用户的输入密码字符串与密码进行比较,以确定输入密码是否正确,然后清除用户的输入密码字符串
  • If the key is "", clear the user’s input password string
    如果密钥为“
    ”,请清除用户的输入密码字符串

Keypad - Password Code 键盘 - 密码代码

/** Created by ArduinoGetStarted.com** This example code is in the public domain** Tutorial page: https://arduinogetstarted.com/tutorials/arduino-keypad*/#include <Keypad.h>const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 3; //three columnschar keys[ROW_NUM][COLUMN_NUM] = {{'1','2','3'},{'4','5','6'},{'7','8','9'},{'*','0','#'}
};byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3}; //connect to the column pinouts of the keypadKeypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );const String password = "1234"; // change your password here
String input_password;void setup(){Serial.begin(9600);input_password.reserve(32); // maximum input characters is 33, change if needed
}void loop(){char key = keypad.getKey();if (key){Serial.println(key);if(key == '*') {input_password = ""; // clear input password} else if(key == '#') {if(password == input_password) {Serial.println("password is correct");// DO YOUR WORK HERE} else {Serial.println("password is incorrect, try again");}input_password = ""; // clear input password} else {input_password += key; // append new character to input password string}}
}
  • Run above code 运行上述代码
  • Open Serial Monitor 开放式串行监视器
  • Press “123456” keys and press “#”
    按“123456”键,按“#”
  • Press “1234” keys and press “#”
    按“1234”键,按“#”
  • See the result on Serial Monitor
    在串行监视器上查看结果

Video Tutorial 视频教程

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.
我们正在考虑制作视频教程。如果您认为视频教程是必不可少的,请订阅我们的 YouTube 频道,为我们制作视频提供动力。

Additional Knowledge 其他知识

  • How to use the multiple passwords for keypad
    如何使用键盘的多个密码
  • How to input a multiple digits number using the keypad
    如何使用键盘输入多位数字

Challenge Yourself 挑战自我

  • Display the pressed key of the keypad on LCD. Hint: Refer to Arduino - LCD
    在 LCD 上显示键盘的按键。提示:请参阅Arduino - LCD
  • Make a door lock with password protection using the keypad.
    使用键盘制作带有密码保护的门锁。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/35859.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

VUE大屏的开发过程(纯前端)

写在前面&#xff0c;博主是个在北京打拼的码农&#xff0c;工作多年做过各类项目&#xff0c;最近心血来潮在这儿写点东西&#xff0c;欢迎大家多多指教。 对于文章中出现的任何错误请大家批评指出&#xff0c;一定及时修改。有任何想要讨论和学习的问题可联系我&#xff1a;1…

tauri使用github action实现跨平台编译并解决编译错误等问题

正常编译为跨平台结果就像上面的&#xff0c;有mac/windows/linux的安装程序&#xff0c;直接下载就可以安装使用&#xff0c;我的这个livebox桌面端仓库地址&#xff1a;GitHub - Sjj1024/LiveBox: livebox&#xff0c;里面有编译文件可以参考。今天主要讲一下遇到的问题。 官…

一投就中,收稿范围大,1个月内录用,国人发文最多,无风险预警

别人费心费力投个一年都不一定有结果&#xff0c;您直接坐上”直升飞机”&#xff0c;1个月录用。下面老毕分享1本超快录用EI期刊&#xff0c;工程电气方向的学者抓紧投稿。 抢占版面&#xff0c;下方【扫一扫】直接安排&#xff0c;1个月内录用&#x1f308; Journal of Elect…

快递大件多少算超重物品?

在快递大件物品时&#xff0c;我们经常听到“超重”这个词&#xff0c;但究竟多重才算超重呢&#xff1f;今天&#xff0c;就让我们来探讨一下快递大件的超重标准&#xff0c;以及如何更经济地快递这些超重物品。 1. 祺祺寄快递&#xff1a; “祺祺寄快递”是一个便捷的快递服…

在开发板上抓包的方法

1.tcpdump tcpdump -i lo -s0 -w /user/lo.pcap tcpdump: 启动 tcpdump 工具&#xff0c;用于捕获网络数据包。-i lo: 指定监听的网络接口为 lo&#xff0c;这里的 lo 是本地回环接口&#xff08;loopback interface&#xff09;&#xff0c;用于本机内部通信。-s0: 设置抓取…

绿盟又行了,漏管市场占有率第一

漏洞管理平台 吉祥学安全知识星球&#x1f517;除了包含技术干货&#xff1a;Java代码审计、web安全、应急响应等&#xff0c;还包含了安全中常见的售前护网案例、售前方案、ppt等&#xff0c;同时也有面向学生的网络安全面试、护网面试等。 今天看到不少朋友圈在转发&#xff…

使用Python实现深度学习模型通常涉及以下几个步骤

学习总结 1、掌握 JAVA入门到进阶知识(持续写作中……&#xff09; 2、学会Oracle数据库入门到入土用法(创作中……&#xff09; 3、手把手教你开发炫酷的vbs脚本制作(完善中……&#xff09; 4、牛逼哄哄的 IDEA编程利器技巧(编写中……&#xff09; 5、面经吐血整理的 面试技…

亚马逊测评干货分享:跨境卖家店铺测评技巧

测评在亚马逊、etsy、temu、速卖通、vinted、ebay、allegro、Jumia、Fruugo、敦煌、shopee、ozon、阿里国际站、沃尔玛、newegg等跨境平台中扮起着重要的方式&#xff0c;卖家们了解到测评可以快速增加产品的销量、评论数量&#xff0c;提升排名&#xff0c;从而打造爆款产品。…

Python基于逻辑回归分类模型、决策树分类模型、随机森林分类模型和XGBoost分类模型实现乳腺癌分类预测项目实战

说明&#xff1a;这是一个机器学习实战项目&#xff08;附带数据代码文档视频讲解&#xff09;&#xff0c;如需数据代码文档视频讲解可以直接到文章最后获取。 1.项目背景 在当今医疗健康领域&#xff0c;乳腺癌作为威胁女性健康的主要恶性肿瘤之一&#xff0c;其早期诊断与精…

Ollama qwen2:7b

简介 一个简明易用的本地大模型运行框架&#xff0c;Ollama官网&#xff1a;Ollama ollama命令 ollama有类似docker的命令。下面是一些模型(large language models)的操作命令: ollama list&#xff1a;显示模型列表ollama show&#xff1a;显示模型的信息ollama pull&#…

2024年【建筑电工(建筑特殊工种)】模拟试题及建筑电工(建筑特殊工种)作业考试题库

题库来源&#xff1a;安全生产模拟考试一点通公众号小程序 2024年建筑电工(建筑特殊工种)模拟试题为正在备考建筑电工(建筑特殊工种)操作证的学员准备的理论考试专题&#xff0c;每个月更新的建筑电工(建筑特殊工种)作业考试题库祝您顺利通过建筑电工(建筑特殊工种)考试。 1、…

visual studio2022配置和使用protobuf

上图证明&#xff0c;我真的测了好多遍&#xff0c;测了好多版本的protobuf&#xff0c;花了很多时间。不过好在最后在vs2022上测通了。 下载protobuf 这里是protobuf下载的地址。 Releases protocolbuffers/protobuf GitHub 个人使用的3.21.9这个版本才跑通的。 1、首先…

2024年5月90篇代码大模型论文最全整理

引言&#xff1a; 本文整理 2024 年 5 月发布的 90 篇代码大模型相关论文&#xff0c;其中包括 17 篇发表在今年 ICLR 的论文。根据论文内容&#xff0c;我们将这些论文整理为了基座模型、代码微调、测试基准、代码 Agent、低资源语言处理、AI 代码安全与分析、人机交互、软件…

Sqlserver双活

要实现Sqlserver双活不是一件简单的事情&#xff0c;什么是双活&#xff0c;就是两边都活着&#xff0c;两边都可以访问&#xff0c;也就是A服务器部署一个sqlserver服务&#xff0c;B服务器部署一个sqlserver服务&#xff0c;两边数据双向同步保持一致&#xff0c;当A数据库服…

EDA 虚拟机 Synopsys Sentaurus TCAD 2018.06-SP2 CentOS7.9

下载地址&#xff08;制作不易&#xff0c;下载使用需付费&#xff0c;不能接受的请勿下载&#xff09;&#xff1a; 链接&#xff1a;https://pan.baidu.com/s/1358rH_Ner1TYdc_TgoXrew?pwdyq3p 提取码&#xff1a;yq3p

2024年【G1工业锅炉司炉】考试及G1工业锅炉司炉考试题库

题库来源&#xff1a;安全生产模拟考试一点通公众号小程序 2024年G1工业锅炉司炉考试为正在备考G1工业锅炉司炉操作证的学员准备的理论考试专题&#xff0c;每个月更新的G1工业锅炉司炉考试题库祝您顺利通过G1工业锅炉司炉考试。 1、【多选题】TSGG0001-2012《锅炉安全技术监察…

Animate软件基础:更改图层的轮廓颜色

在Animate软件中&#xff0c;图层都有单独的轮廓颜色&#xff0c;并且可以进行自定义设置&#xff0c;用来在显示轮廓状态下标记不同的图层。 更改图层轮廓颜色的过程如下&#xff1a; 1.执行如下操作之一&#xff1a; 双击时间轴中图层的图标&#xff08;即该图层名称左侧的…

vite 创建vue3项目 集成 ESLint、Prettier、Sass等

在网上找了一大堆vue3脚手架的东西&#xff0c;无非就是vite或者vue-cli,在vue2时代&#xff0c;vue-cli用的人挺多的&#xff0c;也很好用&#xff0c;然而vue3大多是和vite搭配搭建的&#xff0c;而且个人感觉vite这个脚手架并没有那么的好用&#xff0c;搭建项目时只能做两个…

《昇思25天学习打卡营第2天 | 昇思MindSpore张量 Tensor》

第二天学习 1.今天学习了张量 Tensor&#xff0c;了解到Tensor是一个可用来表示在一些矢量、标量和其他张量之间的线性关系的多线性函数&#xff0c;也是一个特殊的数据结构&#xff0c;与数组和矩阵非常相似。是MindSpore网络运算中的基本数据结构。学些了张量和稀疏张量的属性…

Linux|如何查找和删除重复文件

引言 整理您的个人文件夹甚至整个操作系统可能会相当棘手&#xff0c;特别是当您习惯于使用下载管理器从网上下载各种资料时。 在很多情况下&#xff0c;您可能会发现自己不小心下载了重复的mp3、pdf和epub文件&#xff08;以及其他类型的文件&#xff09;&#xff0c;并将它们…