arduino如何判断串口输入回车键

arduino如何判断串口输入回车键,第1张

你好!要确定一个回车键究竟输入了什么,影响因素很多,比如 *** 作系统的或者输入软件本身的影响。当然了,这也无外乎三种情况:\r, \n或者是\r\n。你需要自己来判断一下具体的情况是怎样的

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

// 44键盘针脚定义根据需要自己修改
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;
// 44键盘的按钮名称
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;
    }
  }
}

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

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

安装44矩阵键盘

44矩阵键盘有一个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 <Keypadh>

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(){

Serialbegin(9600);

}

void loop(){

char customKey = customKeypadgetKey();

if (customKey){

Serialprintln(customKey);

}

}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存