Hi..
I have problem for the keypad.getKey(). Here are the program.
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char answer;
int correct;
int wrong;
long i;
long j;
void setup()
{
//lcd.begin(16,2);
//lcd.clear();
Serial.begin(9600);
//Serial.print(254, BYTE);
//delay(100);
Serial.println("Welcome to MATH GAME!!!"); // Initial dispaly
Serial.println("Press * to start"); // Message on lcd
//delay(2000);
}
void loop()
{
char key = keypad.getKey();
if (key != NO_KEY)
{
switch (key)
{
case '*':
Serial.println("[1] Addition");
Serial.println("[2] Subtraction");
Serial.println("[3] Exit");
Serial.println("Choose");
delay(1000);
break;
default:
Serial.println("Please Press *");
}
}
key = keypad.getKey();
if (key != NO_KEY)
{
switch (key)
{
case '1':
//Serial.println("ONE");
Addition();
break;
case '2':
Serial.println("Subtraction");
break;
default:
Serial.println("Nothing");
}
}
}
void Addition()
{
i = random(10);
j = random(10);
if ((i<9) && (j<9))
{
Serial.print(i);
Serial.print("+");
Serial.print(j);
Serial.print("=");
answer=i+j;
char key = keypad.getKey();
if (key != NO_KEY)
{
if (key == answer)
Serial.println("CORRECT");
else
Serial.println("WRONG");
}
}
//Serial.println(answer);
}
The problem is for the "Press * to start", when I press *, the output is
[1] Addition
[2] Subtraction
[3] Exit
Choose
Then I choose 1, the output is Please Press *. I press 1 for three times..only the third one, the output is correct (0+2)
Please Press *
Please Press *
0+2
Then when I entered the answer, 2, the output is Subtraction. What I want is the output should be "CORRECT".
Actually I do not know how this program works and why this happen?..