arduino 4*4矩阵键盘 怎么用

arduino 4*4矩阵键盘 怎么用,第1张

这里简单介绍一种矩阵键盘的工作原理,4*4矩阵键盘有8个引脚,4个一组,分别对应行和列,通过按键扫描的方法,对不同行(列)分别输入高低电平,然后读取不同列(行)上的电平,从而知道键盘上的某一按键按下。

例如,当第1行握弯输出低电平,其他行输出高电平,分别读取依次列上的状态,如果第1列为低,结果为(1,1),按键为1,如果第2列为低,则结果为(1,2)按键为2

安装4*4矩阵键盘

4*4矩阵键盘有一个8孔的排母,理论上可以直接插到0-7脚上,但0,1脚用于串口通信,所以只能选择2~13脚,这里选用了2-9脚。

首先,选取一个16 PIN 的双排针,将双排针长的那一排的一面引脚插到键盘排母里

另一面插8P线,8P线另一头按键盘正面从左到右的顺序,线接2 PIN排针,再接5 PIN排针,

2 PIN 的排针插到Arduino的8,9脚,5 PIN 的排针插到如皮颂2~5脚

定义Arduino IO口

byte rowPins[ROWS] = {9, 8, 7, 6}//连接到行数字小键盘的渣郑管脚

byte colPins[COLS] = {5, 4, 3, 2}//连接到列数字小键盘的管脚

示例程序

[C++]

#include <Keypad.h>

const byte ROWS = 4//four rows

const byte COLS = 4//four columns

//define the cymbols on the buttons of the keypads

char hexaKeys[ROWS][COLS] = {

{'1','2','3','A'},

{'4','5','6','B'},

{'7','8','9','C'},

{'*','0','#','D'}

}

byte rowPins[ROWS] = {9, 8, 7, 6}//connect to the row pinouts of the keypad

byte colPins[COLS] = {5, 4, 3, 2}//connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad

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矩阵键盘中添加切换功能,可以在同一键盘上设置多个不同的功能。下面是一些基本的步骤:

1. 将矩阵键盘连接至Arduino板。

2. 为每个按键分配一个唯一的键码。例如,将第一行第二列的按键分配为键码1,第一行第三列的按键分配为键码2,以此类推。

3. 定义一个状态变量(例如,状态),并将其初始值设为0。

4. 在主循环中检测矩阵键盘是否有按键被按下。如果有按键按下,则读取该按键的键码,并根据状态吵友变量的值执行相应的 *** 作。

5. 当状态变量值为0时,执行第一组功能;当状态变量值为1时,执行第升返槐二组功能;以此类推。

6. 在主循环中,添加代码来检测切换按钮是否按下。如果检测到切换世空按钮按下,则将状态变量的值加1,但不能超过可用的功能数量,然后等待一定时间(例如200ms)以避免重复按下。

通过以上步骤,您就可以在Arduino矩阵键盘上添加切换功能。在实际应用中,您可以自定义各个按键的功能,以满足您的需求。

没有板子调试,在电脑上自己敲的。在arduino1.8.2上编译通过亩竖,使用了1948字节。你自己看看调调吧。

// 4*4键盘的针脚定义根据需要自己修改

const int Pins[] = {1,2,3,4,5,6,7,8}

// 接收按键状态的数组

int PressState[] = {0,0,0,0,0,0,0,0}

// 假设输出用状态灯(三色或三个),outPins[0]高空闲等待输入,outPins[1]高正确,outPins[1]高错误,全灭为正在输入

const int outPins[] = {9,10,11}

int currOut = 0

// 4*4键盘的按钮名称

const char BottonNames[4][4] = 

  {{'1','2','3','A'},

  {'4','5','6','B'},

  {'7','8','9','C'},

  {'*','0','#','D'}}

// 确认按键

const char EntterBotton = 'D'

char lastKey = 0//上次的按键

// 内置默认密码

const char *pPasswd = "1359A"

const int passwdLen = strlen(pPasswd)//正确的密码长度

char pBuff[8] = ""// 注意至少比pPasswd长1

int currLen = 0

// 按键间隔时间,500毫秒内相同按键视为一次按键

const unsigned long PressTime = 500

// 超时不 *** 作认为放弃

const unsigned long TimeOut = 5000// 超时时间

unsigned long currTime = 0// 当前时间

unsigned long lastTime = 0// 上次 *** 作时间

int i=0

// 设置输出状态灯

void setLight(int light){

  digitalWrite(outPins[0], LOW)

  digitalWrite(outPins[1], LOW)

  digitalWrite(outPins[2], LOW)

  if (light > -1 && light < 3) {

    digitalWrite(outPins[light], HIGH)

  }

}

// 清除状态记录

void clear(){

  currOut = 0

  currLen = 0

  pBuff[0] = 0

}

// 获取按键值

char getKey(int *pPressState){

  // 只有一个按键按下认为有效

  if ((HIGH == pPressState[0]+pPressState[1]+pPressState[2]+pPressState[3]) 

  && (HIGH == pPressState[4]+pPressState[5]+pPressState[6]+pPressState[7])){

    int x = 0

    int y = 0

    for (i=0 i<4 i++) {

      if (HIGH == pPressState[i]) {

        x = i

      }

      if (HIGH == pPressState[i+4]) {

        y = i

      }

    }

    return BottonNames[x][y]

  }

  // 没按多按直接返回没有按

  return 0

}

void setup() {

  // 设置键盘的针脚为输入针脚

  for (i=0 i<sizeof(Pins)/sizeof(Pins[0]) i++){

    pinMode(Pins[i], INPUT)

  }

  // 设置状态灯针脚

  for (i=0 i<sizeof(outPins)/sizeof(outPins[0]) i++){

    pinMode(outPins[i], OUTPUT)

  }

  clear()

  setLight(currOut)

}

void loop() {

  // 判断超时

  if (currOut<0){

    currTime = millis()

    if (currTime-lastTime > TimeOut) {

      clear()

      setLight(currOut)

    }

  }

  // 获取按键输入

  for (i=0 i<sizeof(Pins)/sizeof(Pins[0]) i++){

    PressState[i] = digitalRead(Pins[i])

  }

  // 提取按键信息

  char key = getKey(PressState)

  // 没有亏竖(新)按键

  if (0 == key || lastKey == key) {

    delay(50)

    return

  }

  lastKey = key

  currOut = -1

  // 如果按下确认键开始校验

  if (EntterBotton 迅空大== key){

    clear()

    // 密码正确

    if (currLen == passwdLen && 0 == strncmp(pBuff,pPasswd,passwdLen)){

      currOut = 1

    // 密码错误

    }else{

      currOut = 2

    }

    // 显示结果

    setLight(currOut)

  }else{

    // 记录输入的内容和长度

    lastTime = currTime

    if (currLen++ < passwdLen){

      pBuff[currLen-1] = key

    }

  }

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12227218.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-22
下一篇 2023-05-22

发表评论

登录后才能评论

评论列表(0条)

保存