Arduino Multi tap功能混乱

时间:2014-10-04 22:58:12

标签: arrays arduino numeric-keypad

我仍然是c ++编程世界的新手,所以对我们来说很容易。哈哈

我试图建立一个系统来跟踪我父亲最喜欢的游戏中的高分。我使用液晶显示屏和3x4键盘进行分数输入。我想在键盘上添加一个多次击键功能,这样就好像它是一个用于存储用户首字母的电话键盘。我已经创建了一个用于存储分数的功能(它可能不是最好的方法,但它有效,关于如何改进的评论将非常感激。)我甚至不知道从哪里开始。我已经看过Arduino键盘库中的动态键盘示例,但我觉得我可能比我可以咬的更多 我的代码如下:

#include <SoftwareSerial.h>
#include "Keypad.h"

// Create a software serial port!
SoftwareSerial lcd = SoftwareSerial(11,10); 
char scoreinput[] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
int counter = -2;
int counter2=0;
uint8_t red, green, blue;



const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {5, 6, 7, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {2, 3, 4}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


void setup() {
lcd.begin(9600); 
Serial.begin(9600);

lcd.write(0xFE); //clear display 
lcd.write(0x58);
delay(10);
lcd.print("****PinScore*******************");
lcd.write(0xFE);//set cursor at home
lcd.write(0x48);
delay(5000);



lcd.write(0xFE); //clear display 
lcd.write(0x58);
delay(10);
lcd.print("New High Score?");
lcd.write(0xFE);
lcd.write(0x47);
lcd.write((uint8_t)2);
lcd.print(" Press      \"#\"");
delay(5000);

lcd.write(0xFE); //clear display 
lcd.write(0x58);
delay(10);
lcd.print("Display Scores?");
lcd.write(0xFE);
lcd.write(0x47);
lcd.write((uint8_t)2);
lcd.print(" Press      \"*\"");
delay(5000);

lcd.write(0xFE); //clear display 
lcd.write(0x58);
lcd.print("     Press     ");
lcd.write(0xFE);
lcd.write(0x47);
lcd.write((uint8_t)2);
lcd.print(" \"*\"   or  \"#\" ");
}


void loop() {



char key = keypad.getKey();

if (key != NO_KEY){
Serial.print(key);
lcd.print(key);
counter++;
counter2++;
scoreinput[counter]=(key);
}

switch (key == '#'){
   case 1:




lcd.write(0xFE); //clear display 
lcd.write(0x58);
delay(10);
lcd.print("Enter Your New  High Score");
delay(3000);
lcd.write(0xFE); //clear display 
lcd.write(0x58);
lcd.print("Press \"#\" At Any Time To Reset");
delay(3000);
lcd.write(0xFE); //clear display 
lcd.write(0x58);
lcd.print("Enter High Score:");


lcd.write(0xFE);
lcd.write(0x47);
lcd.write((uint8_t)18);
lcd.print(key);

if (counter==8){
  break;
}
}

if (counter==8){
lcd.write(0xFE); //clear display 
lcd.write(0x58);
delay(1000);
lcd.print(scoreinput[0]);
lcd.print(scoreinput[1]);
lcd.print(scoreinput[2]);
lcd.print(",");
lcd.print(scoreinput[3]);
lcd.print(scoreinput[4]);
lcd.print(scoreinput[5]);
lcd.print(",");
lcd.print(scoreinput[6]);
lcd.print(scoreinput[7]);
lcd.print(scoreinput[8]);
counter=-2;
delay(3000);
lcd.write(0xFE); //clear display 
lcd.write(0x58);
lcd.print("Enter your      Initals:");
  }
while (counter2>10){
if (key == '2'){
//this is where the multi tap function would be called to allow users to enter their intials 

}
}

1 个答案:

答案 0 :(得分:0)

您只需要一个计时器来衡量自上次键输入以来的时间。如果自上次击键以来已经超过一定的时间(可能是1秒),或者如果键与推送的最后一个键不同,则输入 new 字符(键的第一个字符; 2 = A,3 = D等,否则它增加字符(具有环绕,A =&gt; B,B => C,C =&gt; A)。所以&#34; 234&#34;进入&#34; ADG&#34;和&#34; 222&#34;进入&#34; C&#34;但是&#34; 2(延迟)22&#34;进入&#34; AB&#34;。