#include <Keypad.h>
#define ROW_NUM 4
#define COL_NUM 4
long int currentTime = 0;
long int previousTime = 0;
int threshold = 4000;
int score = 0;
char key = '\0';
char keys[ROW_NUM][COL_NUM] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
byte rowPins[ROW_NUM] = { 19, 18, 5, 17 };
byte colPins[COL_NUM] = { 16, 4, 2, 15 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROW_NUM, COL_NUM);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
byte rand_row_num = random(1, 5);
byte rand_col_num = random(1, 5);
char rand_element = keys[rand_row_num-1][rand_col_num-1];
String hintRow = "Row: " + String(rand_row_num);
String hintCol = "Column: " + String(rand_col_num);
byte rand_hint = random(1, 3);
if (rand_hint == 1) {
Serial.println(hintRow+"\t"+hintCol);
} else {
Serial.println(hintCol+"\t"+hintRow);
}
while (true) {
char k = keypad.getKey();
if (k) {
key = k;
Serial.println(key);
}
currentTime = millis();
if (currentTime - previousTime == threshold) {
if (key == rand_element) {
score++;
if (score > 5) {
Serial.println("You win!");
while(true);
}
Serial.print("Correct! Your score is: " + String(score));
threshold = threshold - 200;
} else {
Serial.println("You lost!");
while(true);
}
previousTime = currentTime;
break;
}
}
}