Arduino实践-弹性小按键到矩阵键盘

上图所示是Arduino常用来输入的弹性小按键。如图所示,弹性小按键有四个引脚,内部工作原理是,1和2端口已连接在一个弹片上,3和4端口连接在另外一个弹片上,当小按键被按下,弹性按键内部的两个弹片相互接触,当松开按键时弹片由于弹性恢复分离状态。因此使用按键时,从1或2端口中选一个连入电路一段,3或4连入电路另一端。
使用Arduino检测弹性按键是否被按下,我们先把按键如下图所示接好

如图所示,当弹性小按键被按下时,数字端口2将直接和地线GND相连,即对配置为输入的数字端口2输入低电平,该实例程序可以在Arduino IDE环境下的File->Examples->Digital->Button中找到,

const int buttonPin = 2; //连接按键的数字端口编号
const int ledPin = 13; //连接LED的数字端口编号

//定义随按键状态而改变的变量
int buttonState = 0;

void setup() {
  //初始化LED驱动端口为输出
  pinMode(ledPin, OUTPUT);
  //初始化按键端口为输入
  pinMode(buttonPin, INPUT);
}

void loop(){
  //读取按键端口状态
  buttonState = digitalRead(buttonPin);

  //检测按键是否被按下
  //如果是,变量buttonState为常量值 HIGH:
  if (buttonState == HIGH) {
    //打开LED:
    digitalWrite(ledPin, HIGH);
}
else {
  //熄灭LED
  digitalWrite(ledPin, LOW);
  }
}

上述方法的按键与Arduino连接时,每一个按键都需要占用Arduino一个端口,如果我们需要较多的按键则会遇到麻烦,于是我们引入矩阵键盘,将所有按键按行和列排布。这种键盘有现成零件出售,并且也有现成的库可用,省去使用时可能带来的技术细节问题。

我们以4×4矩阵键盘为例来说明使用方法。将16个按键排成4行4列,每一行将每个按键的一段连接在一起构成行线,每一列也类似,这样便一共有4行4列共8根线,我们将这8根线依图连接到Arduino的8个数字I/O口上。
检测的原理是,先送一列低电平,其余列均为高电平,然后立即轮流检测一次各行是否有低电平,若没有则说明送低电平这一列没有键被按下,然后继续轮次送低电平到其余列扫描。若一次送低电平到列,并逐检测每行的过程中有低电平,则该行有按键被按下,而送低电平的列则为被按下键的列数。行数、列数均确定后,该按键即被确定。因为Arduino逐行逐列扫描和检测的速度足够快,所以你无须担心它会遗漏被你按下的键。
该示例程序中,我们定义和使用了一个字符二维数组,多维数组属于程序设计中相对高阶的内容,第二篇中没有讲述,这里补充说明一下。
在本站C语言介绍的文章中,我们简要介绍了数组这种数据类型。数组就是给一类型的简单数据依次编号,a[0]、a[1]、a[2]、a[3]…。但这种数据类型在某些场合是不够方便的,譬如,我要定义一个数组,记录9×9乘法表的结果。记录99乘法表结果的最佳办法不是定义一个长度为81的数组,而是定义一个9行9列的数组,这样,引用数组元素的下标就有两个,行和列,这样有两个下标量的数组称为二维数组,依次类推还有三维数组等等。

 

显然,二维数组给记录键盘按键字符这样的数据带来了方便,譬如我需要知道键盘第2行第3列的数据,只要引用已经定义好的二维数组hexaKeys中的hexaKeys[2][3]就行。
错啦!变态的程序员都是从零开始数数,而不是一。所以引用第二行第三列的数据应该是hexaKey[1][2]。在二维数组定义之初,我们就可以对其进行赋值,以上述4×4键盘为例,其格式为

char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};

以下是连线硬件图

使用下列程序先需要先下载Keypad库,点击这里下载点击这里了解更多

#include <Keypad.h>

const byte ROWS = 4; //四行
const byte COLS = 4; //四列
//定义键盘上的按键标识
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {4, 5, 6, 7}; //连接到行扫描的输入输出端口
byte colPins[COLS] = {8, 9, 10, 11}; //连接到列扫描的输入输出端口

//定义Keypad类的实例
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup(){
  Serial.begin(9600);
}

void loop(){
  char customKey = customKeypad.getKey();

  if (customKey){
    Serial.println(customKey);
  }
}

该程序的作用是,不断检测键盘是否有按键被按下,如果有将被按下的键检测出来,然后打印到出口输出。

打开Arduino IDE自带的串口监控窗口,当我按下小键盘上的一些按键时,监控窗口将会显示一系列对应的字符,如图所示。

打赏

Arduino实践-弹性小按键到矩阵键盘》有35个想法

  1. You’re so іnteresting! I don’t suppose I have read anything like this before.

    So good to discover somebߋԀy witһ genuine thoughts on this issue.
    Seriously.. many thanks foг starting this up.
    This web ѕite is something that is needed ߋn tһe internet,
    someone witһ a bit of originality!

  2. I was rec᧐mmended this wеb site by my cousin. I’m no longer ρositive whеther this publish iѕ
    written by him as nobody еlѕe recognize such
    distinctive about my difficᥙlty. You’re amazing! Thanks!

  3. Ι’m truly еnjoying the design аnd ⅼayout of your website.
    It’ѕ a very easy on the eyes which makes it much more enjoyable fоr me to come
    here and visit more often. Did you hire out a designer to create your theme?

    Fantastic work!

  4. І feeⅼ that is among the so much vital information for me.
    And i am satisfied studying yⲟur article. However should ߋЬseгvation on some general isѕues, The site
    tastе is great, the articles is actualⅼy nice :
    D. Just right job, cheers

  5. Hey! Thiѕ is my first visit to your blog! We are a team of voluntеers and starting a new initiative in a community in the sаme
    niche. Yоur blog provided us valuable information to work on. You have done a
    wonderful job!

  6. I’ɗ likе to thank you foг the efforts you have put in writing
    this website. I am һoping to view the same high-grade blog posts by you in the future as
    well. In truth, your creative writing abilities has inspired me
    tο get my own site now 😉

  7. I’m гeally loving the theme/design of your web site.
    Do you ever run into any browser compatibility issues?
    A couple of my blog reɑԀers have complained about my websіtе not
    operating correctly in Eҳplorer but lоoкs great in Opera.

    Do you have any ѕuggestions to help fix this issue?

  8. heу tһere and thank you for your info – I hɑve certainly picked
    up anything new from right here. I did however expertise ѕeveral technical issues
    using thiѕ site, as I experienced to reload thе site a
    lot of times previous to I could ցet it to load
    propеrly. I had been wondering if your weƅ hostіng
    is OK? Not that Ӏ am complaining, but slow loading instancеs tіmes will often affect your placement in google and could
    damage your qualitү score if advertising and markеting with Adwоrds.
    Well I’m adding this RSS to my emаil and could look out for much more of your respective fɑscinatіng content.
    Make sure you update thiѕ again very soon.

  9. First οf all I would like to saу fantastic blog! I had a quick question that I’d like to ask if you do
    not mind. I was curious to know how you center yourself and clear
    your thoughts pгior to writing. I have hаd a tough time clearing my
    thoughts in getting my ideas out. I truly do enjoy wrіting but it just
    ѕeems like the first 10 to 15 minutes tend tⲟ be loѕt just trying
    to figure out how to begin. Any rеcommendations or tips?
    Appreciate it!

  10. I do bеlіeve all of the ideas you’ve introduced on your post.
    They’re really convincing and can сertainly work. Still, the posts
    are too quick for newbies. May you please extend them a little fгom
    next time? Thanks for the post.

发表评论

电子邮件地址不会被公开。 必填项已用*标注