想把4*4键盘上输入的字符转化成字符串,请大神帮忙看下程序哪里出问题了
本帖最后由 凯风自北来 于 2015-8-4 18:51 编辑输入2#之后串口显示是这样的,请问程序哪里出了问题
#include <Keypad.h>
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()
{
customKey = customKeypad.getKey();
String res="";
while( customKey !='#')
{ customKey = customKeypad.getKey();
res += char(customKey);delay(2);
}
Serial.print(res);
} while(customKey !='#')
这儿有问题,也就是说,只要你没输入‘#’,就会一直在while循环里面,res会一直变大,又由于你什么都没输入,所以res字符串中才会有这么多空字符。
一般键盘的程序都是当有按键按下后才记录字符。
String res="";
void loop()
{
customKey = customKeypad.getKey();
if(customKey=='#')
{
Serial.print(res);
}
else if(customKey!=' ')
{
res += char(customKey);
delay(2);
}
} 164335413 发表于 2015-8-4 08:46 static/image/common/back.gif
while(customKey !='#')
这儿有问题,也就是说,只要你没输入‘#’,就会一直在while循环里面,res会一直变 ...
我刚刚试了下,没有输入和空格' '不等效 164335413 发表于 2015-8-4 08:46 static/image/common/back.gif
while(customKey !='#')
这儿有问题,也就是说,只要你没输入‘#’,就会一直在while循环里面,res会一直变 ...
这样是正确的String res="";
void loop()
{
customKey = customKeypad.getKey();
if(customKey=='#')
{
Serial.print(res);
}
else if(customKey!=NO_KEY)
{
res += char(customKey);
delay(100);
}
}
页:
[1]