请教大神如何将4*4键盘输入的字符转化成字符串
本帖最后由 凯风自北来 于 2015-8-4 18:51 编辑#include <Keypad.h>
floatflowrate = 1000;
floatdiameter = 8.24;
floatarea;
float velocity;
char customKey;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char hexaKeys = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins = {9, 8, 7, 6};
byte colPins = {5, 4, 3, 2};
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
}
void loop()
{
if(customKeypad.getKey() !=NO_KEY)
{ sysConfig();
area=3.14*pow((diameter/2),2);
velocity=(flowrate/area)*20/3;
Serial.print("velocity = ");
Serial.print(velocity);
Serial.println(" steps/s");}
}
void sysConfig(){
String res="";
String data="";
while( customKeypad.getKey() !='#')
{
customKey = customKeypad.getKey();
res += char(customKey);delay(100);
}
Serial.println("Input: ");
Serial.println(res);
Serial.println("----------------------");
Serial.println("Output: ");
for(int i=0,j=0,l=res.length();i<l;i++){
if(res == '*') j++;
else data += char(res);
if(j > 3) break;
}
if(data=="A"){
flowrate= atof(data.c_str());
Serial.print( "flowrate = ");
Serial.print(flowrate);
Serial.println(" uL/min ");
}
if(data=="B"){
diameter= atof(data.c_str());
Serial.print( "diameter = ");
Serial.print( diameter);
Serial.println(" mm ");
}
else{
Serial.println("Unknown command");
}
Serial.println("----------------------");
}
输入A*1000*B*10#后显示如下,请问是哪里出错了
while( customKeypad.getKey() !='#')
{
customKey = customKeypad.getKey();
res += char(customKey);delay(100);
}
这一句仍然很别扭,如果你键盘不输入#,那就会一直停在这一句,这是不合理的。
你可以尝试用if(customKeypad.getKey() !='#')
当然,下面的语句肯定是你按下 #后想要执行的句子 164335413 发表于 2015-8-4 08:50 static/image/common/back.gif
while( customKeypad.getKey() !='#')
{
customKey = customKeypad.getKey();
所以想输入A*1000*B*10时会在后面加上#作为结束的标志 164335413 发表于 2015-8-4 08:50 static/image/common/back.gif
while( customKeypad.getKey() !='#')
{
customKey = customKeypad.getKey();
我的本意是按键按下一串字符存成字符串形式,#作为我输入结束的标志但不是输入内容。我这么写我也觉得别扭且得不到正确结果,但是不清楚问题出在哪里了。
另外根据你的提示改写的程序是对的!多谢大神,还劳大神在指点下我原来的写法错到哪里了 虽然我不清楚keyboard.h库里面到底有什么,但你想要的是键值 customKey,只要你按下按键, customKey里面的字符就会改变。
所以获得键值应该在
void loop()
{
customKey = customKeypad.getKey();
.....
}
剩下的就是需要你去判断
if( customKey=='#')
{
print("......")
//当判断键值为‘#’时,就结束字符输入(捎带把res清空,最好把res设置成全局变量)
}
if(customKey==' ')//因为在不输入字符时,customKey里面有两种可能(空字符或者你上次输入的字符,这要根据customKeypad.getKey();函数返回的情况而定)
164335413 发表于 2015-8-4 10:56 static/image/common/back.gif
虽然我不清楚keyboard.h库里面到底有什么,但你想要的是键值 customKey,只要你按下按键, customKey里面的 ...
while错在哪里呢 一群字符不就是字符串了么{:soso_e143:}
页:
[1]