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 columns

char 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 keypad

Keypad 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 columns

char 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 keypad

Keypad 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 columns

char 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 keypad

Keypad 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.mfbz.cn/a/752423.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Kompas AI用户体验与界面设计对比

一、引言 在人工智能&#xff08;AI&#xff09;产品领域&#xff0c;用户体验&#xff08;UX&#xff09;和界面设计&#xff08;UI&#xff09;是衡量产品成功与否的两个关键指标。一个优秀的AI产品不仅需要具备强大的功能&#xff0c;还需要提供流畅、直观且富有吸引力的用…

还不会写WorkFlow?“讲课“即工作流,摩根大通用一段Prompt诱导LLMs自主生成

随着各种自动生成Prompt的工具被开源&#xff0c;Prompt Engineer的生存空间也在不断被压缩&#xff0c;一个明显的转变已经出现&#xff1a;要想在ALL IN AI的状态下生存下去&#xff0c;你要能从Prompt Engineer切换成WorkFlow Engineer。而WorkFlow领域的竞争也是非常激烈的…

CSS 核心知识点 - grid

思维导图 参考网址: https://developer.mozilla.org/zh-CN/docs/Web/CSS/CSS_grid_layout 一、什么是 grid&#xff1f; CSS Grid布局是在CSS3规范中引入的一种新的布局方式&#xff0c;旨在解决传统布局方法&#xff08;如浮动、定位、表格布局&#xff09;存在的许多问题。C…

【STM32修改串口波特率】

STM32微控制器中的串口波特率调整通常涉及到USART&#xff08;通用同步接收器/发送器&#xff09;模块的配置。USART模块提供了多个寄存器来设置波特率&#xff0c;其中关键的寄存器包括BRR&#xff08;波特率寄存器&#xff09;和USART_CR1&#xff08;控制寄存器1&#xff09…

【数学建模】——【python库】——【Pandas学习】

专栏&#xff1a;数学建模学习笔记 pycharm专业版免费激活教程见资源&#xff0c;私信我给你发 python相关库的安装&#xff1a;pandas,numpy,matplotlib&#xff0c;statsmodels 总篇&#xff1a;【数学建模】—【新手小白到国奖选手】—【学习路线】 第一卷&#xff1a;【数学…

推荐系统中冷启动环节的设计实现

推荐系统中的冷启动分为物料冷启动和用户冷启动。用户冷启动主要是针对新用户&#xff0c;但有时候也用于低活用户拉活。物料冷启动主要是让优质物料得到快速下发&#xff0c;让模型可以迅速捕获到用户对该物料的关注。本文将详细讲解用户冷启动和物料冷启动。 1、用户冷启动 用…

SAMformer:通过锐度感知最小化和通道注意力解锁变换器在时间序列预测中的潜力

目录 摘要1. 引言当前方法的局限性变换器的可训练性我们贡献的总结 2. 提出的方法符号说明2.1 问题设置2.2 激励示例命题2.1&#xff08;最优解的存在性&#xff09; 2.3 变换器的损失景观现有的解决方案 2.4. SAMformer&#xff1a;集成所有方法 3. 实验3.1 主要收获 摘要 基…

【Linux系统编程】进程控制(创建、退出、等待、替换)

目录 再聊进程创建 进程终止 进程等待 进程程序替换 再聊进程创建 初识进程创建 关于进程创建&#xff0c;这里只会说结论&#xff0c;在上面这篇文章中对进程创建进行了比较详细的阐述&#xff0c;而接下来要介绍的&#xff0c;都是基于上文说过的来展开的 一些较为重要…

98%企业竟存N日漏洞超5年,新漏洞利用攻击时长极速缩短!

专注推动网络与安全融合的全球网络安全领导者 Fortinet&#xff08;NASDAQ&#xff1a;FTNT&#xff09;&#xff0c;近日发布 FortiGuard Labs&#xff08;Fortinet全球威胁情报响应与研究团队&#xff09;《2023 下半年全球威胁态势研究报告》。本次新发布的半年度研究报告&a…

MySQL8 新特性——公用表表达式用法 with t1 as (select * from user)

MySQL8 新特性——公用表表达式用法_mysql ctes-CSDN博客 1.普通公用表表达式 MySQL8 新特性——公用表表达式用法 在MySQL 8.0及更高版本中&#xff0c;引入了公用表表达式&#xff08;Common Table Expressions&#xff0c;CTEs&#xff09;&#xff0c;它是一种方便且可重…

Echarts地图实现:杭州市困难人数分布【动画滚动播放】

Echarts地图实现&#xff1a;杭州市困难人数分布 实现功能 杭州市地区以及散点图分布结合的形式数据展示动画轮播可进去杭州市下级地区可返回杭州市地图展示 效果预览 实现思路 使用ECharts的地图和散点图功能结合实现地区分布通过动画轮播展示数据变化实现下级地区数据的展…

深度学习论文: VanillaNet: the Power of Minimalism in Deep Learning

深度学习论文: VanillaNet: the Power of Minimalism in Deep Learning VanillaNet: the Power of Minimalism in Deep Learning PDF:https://arxiv.org/pdf/2305.12972 PyTorch: https://github.com/shanglianlm0525/PyTorch-Networks 1 概述 提出的VanillaNet通过简化设计&…

《数字图像处理与机器视觉》案例二(基于边缘检测和数学形态学焊缝图像处理)

一、前言 焊缝是评价焊接质量的重要标志&#xff0c;人工检测方法存在检测标准不统一&#xff0c;检测精度低&#xff0c;焊缝视觉检测技术作为一种重要的质量检测方法&#xff0c;正逐渐在各行各业中崭露头角。把焊缝准确的从焊接工件中准确分割出来是焊缝评价的关键一步&…

API接口示例的设计与实现技巧?如何编写?

API接口示例怎么使用&#xff1f;哪些工具可以生成API接口示例&#xff1f; 一个良好的API接口示例可以显著提升开发效率&#xff0c;改善用户体验&#xff0c;并确保系统的稳定性和可扩展性。AokSend将探讨API接口示例的设计与实现技巧&#xff0c;帮助开发者构建高质量的API…

使用el-amap-info-window遇到的问题

使用的这个库https://github.com/yangyanggu/vue-amap 想要滚动amapInfoWindow里的内容&#xff0c;但不触发地图缩放 默认滚动amapInfoWindow里的内容&#xff0c;会触发地图缩放。看了C站一个大佬的文章解决了。 amapInfoWindow会自动滚动到顶部 我的amapInfoWindow里面用了…

Spring AI 接入OpenAI大模型实现同步和流式对话

接入前准备 第一&#xff0c;准备OpenAI API Key&#xff0c;如果你可以科学上网&#xff0c;可以参照[# 如何获得Open ai key]这篇文章在 OpenAI 官方网站上获取 OpenAI API Key。 第二&#xff0c;如果不能科学上网&#xff0c;我们可以通过一些代理商获取OpenAI API Key&a…

使用gradle上传maven工件到新版maven central仓库central.sonatype.com

本文主要用到的插件是sonatype-uploader, 该插件主要功能是上传依赖文件夹到中央仓库。 该文件夹的生成也十分简单&#xff0c;不用担心。 前言 最近在研究maven插件的时候发现发布的网站发生了变化&#xff0c;使用之前的一些插件没能满足我发布依赖的需求&#xff0c;也可…

Reflexion:通过语言反馈增强的智能体

Reflexion: Language Agents with Verbal Reinforcement Learning Reflexion: language agents with verbal reinforcement learninghttps://proceedings.neurips.cc/paper_files/paper/2023/hash/1b44b878bb782e6954cd888628510e90-Abstract-Conference.html 1.概述 最近,Re…

海豚调度调优 | 如何解决任务被禁用出现的Bug

&#x1f4a1; 本系列文章是 DolphinScheduler 由浅入深的教程&#xff0c;涵盖搭建、二开迭代、核心原理解读、运维和管理等一系列内容。适用于想对 DolphinScheduler了解或想要加深理解的读者。 祝开卷有益。 本系列教程基于 DolphinScheduler 2.0.5 做的优化。&#xff…

在 AI 公司入职一个月的体验与感悟

已经在一家 AI 公司入职了一个月&#xff0c;对坐班有些厌恶的我&#xff0c;没想到有一天也会开始通勤打卡。而经历了这一个月的工作&#xff0c;我对坐班的态度有所转变&#xff0c;开始理解这种工作方式对我的意义。是时候分享入职这期间的工作内容与感受。 背景 直入正题…
最新文章