arduino 矩阵肩膀_【Arduino基础教程】4*4矩阵键盘

本文是一篇Arduino基础教程,介绍如何连接和编程4*4矩阵键盘。内容包括准备材料、接线、安装库文件、初始化设置、常用函数及示例代码,帮助初学者理解并实现矩阵键盘的操作。
摘要由CSDN通过智能技术生成

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

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参考文献

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Arduino是一种开源电子原型平台,可以用来搭建各种智能设备和项目。它的灵活性和可编程性,使得它成为制作密码锁的理想选择。要构建一个基于Arduino的4*4矩阵键盘密码锁非常简单。 首先,我们需要一个4*4矩阵键盘,它包括16个按键,可以输入0-9的数字和一些特殊符号。然后,我们将键盘与Arduino连接起来,通过Arduino的引脚来读取按键的状态。接下来,我们需要编写Arduino代码,来实现密码锁的功能。 在代码中,我们可以定义一个密码,当用户按下正确的密码时,密码锁就会解锁。我们需要使用一些变量来存储用户输入的密码和当前的密码状态。当用户按下按键时,我们需要将按键的值添加到密码中,并且每次按下按键都需要更新密码锁的状态。当密码输入完成后,我们可以将输入的密码与预设的密码进行比对,如果一致,则密码锁就会解锁。 另外,我们还可以在代码中加入一些安全功能,比如设置最大输入次数和输入超时功能,以防止密码被暴力破解。最后,我们将代码上传到Arduino上,连接电源,密码锁就可以正常使用了。 通过使用Arduino和4*4矩阵键盘,我们可以很容易地制作一个简单的密码锁,保护我们的物品和隐私。同时,Arduino的可编程性也使得我们可以根据自己的需求来定制密码锁的功能和设计。这种DIY的方式不仅有趣,而且可以提高我们的电子制作能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值