极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13686|回复: 6

请教大神如何将4*4键盘输入的字符转化成字符串

[复制链接]
发表于 2015-8-3 21:44:50 | 显示全部楼层 |阅读模式
本帖最后由 凯风自北来 于 2015-8-4 18:51 编辑
  1. #include <Keypad.h>
  2. float  flowrate = 1000;            
  3. float  diameter = 8.24;            
  4. float  area;   
  5. float velocity;
  6. char customKey;
  7. const byte ROWS = 4; //four rows
  8. const byte COLS = 4; //four columns
  9. char hexaKeys[ROWS][COLS] = {
  10.   {'1','2','3','A'},
  11.   {'4','5','6','B'},
  12.   {'7','8','9','C'},
  13.   {'*','0','#','D'}
  14. };
  15. byte rowPins[ROWS] = {9, 8, 7, 6};
  16. byte colPins[COLS] = {5, 4, 3, 2};
  17. Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

  18. void setup() {
  19.     Serial.begin(9600);
  20.   
  21. }
  22. void loop()
  23. {
  24. if(customKeypad.getKey() !=NO_KEY)
  25.    { sysConfig();
  26.     area=3.14*pow((diameter/2),2);
  27.     velocity=(flowrate/area)*20/3;
  28.     Serial.print("velocity = ");
  29.     Serial.print(velocity);
  30.     Serial.println(" steps/s");}
  31. }

  32. void sysConfig(){
  33.   String res="";
  34.   String data[4]="";  
  35.   while( customKeypad.getKey() !='#')
  36.   {
  37.     customKey = customKeypad.getKey();
  38.     res += char(customKey);delay(100);
  39.   }
  40.   Serial.println("Input: ");
  41.   Serial.println(res);
  42.   Serial.println("----------------------");
  43.   Serial.println("Output: ");
  44.   for(int i=0,j=0,l=res.length();i<l;i++){
  45.     if(res[i] == '*') j++;
  46.     else data[j] += char(res[i]);   
  47.     if(j > 3) break;
  48.   }
  49.   if(data[0]=="A"){
  50.    flowrate= atof(data[1].c_str());
  51.    Serial.print( "flowrate = ");
  52.    Serial.print(flowrate);
  53.    Serial.println(" uL/min ");
  54.   }

  55.   if(data[2]=="B"){
  56.     diameter= atof(data[3].c_str());
  57.     Serial.print( "diameter = ");
  58.     Serial.print( diameter);
  59.     Serial.println(" mm ");
  60.   }
  61.   else{
  62.      Serial.println("Unknown command");
  63.   }
  64.   Serial.println("----------------------");
  65. }
复制代码

输入A*1000*B*10#后显示如下,请问是哪里出错了

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2015-8-4 08:50:44 | 显示全部楼层
while( customKeypad.getKey() !='#')
  {
    customKey = customKeypad.getKey();
    res += char(customKey);delay(100);
  }
这一句仍然很别扭,如果你键盘不输入#,那就会一直停在这一句,这是不合理的。
你可以尝试用if(customKeypad.getKey() !='#')
当然,下面的语句肯定是你按下 #  后想要执行的句子
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-8-4 09:40:17 | 显示全部楼层
164335413 发表于 2015-8-4 08:50
while( customKeypad.getKey() !='#')
  {
    customKey = customKeypad.getKey();

所以想输入A*1000*B*10时会在后面加上#作为结束的标志
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-8-4 10:36:44 | 显示全部楼层
164335413 发表于 2015-8-4 08:50
while( customKeypad.getKey() !='#')
  {
    customKey = customKeypad.getKey();

我的本意是按键按下一串字符存成字符串形式,#作为我输入结束的标志但不是输入内容。我这么写我也觉得别扭且得不到正确结果,但是不清楚问题出在哪里了。

另外根据你的提示改写的程序是对的!多谢大神,还劳大神在指点下我原来的写法错到哪里了
回复 支持 反对

使用道具 举报

发表于 2015-8-4 10:56:38 | 显示全部楼层
虽然我不清楚keyboard.h库里面到底有什么,但你想要的是键值 customKey,只要你按下按键, customKey里面的字符就会改变。
所以获得键值应该在
void loop()
{
customKey = customKeypad.getKey();
.....

}
剩下的就是需要你去判断
if( customKey=='#')
{
print("......")
//当判断键值为‘#’时,就结束字符输入(捎带把res清空,最好把res设置成全局变量)
}
if(customKey==' ')//因为在不输入字符时,customKey里面有两种可能(空字符或者你上次输入的字符,这要根据customKeypad.getKey();函数返回的情况而定)
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-8-4 11:46:23 | 显示全部楼层
164335413 发表于 2015-8-4 10:56
虽然我不清楚keyboard.h库里面到底有什么,但你想要的是键值 customKey,只要你按下按键, customKey里面的 ...

while错在哪里呢
回复 支持 反对

使用道具 举报

发表于 2015-8-4 14:22:43 | 显示全部楼层
一群字符不就是字符串了么{:soso_e143:}
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-8 05:27 , Processed in 0.078921 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表