#include <Keypad.h>
#define ROW_NUM 4
#define COL_NUM 4
byte score = 0;
byte chances;
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() {
Serial.begin(9600);
}
void loop() {
chances = 5;
byte row = random(0, 4);
byte col = random(0, 4);
char rand_elem = keys[row][col];
Serial.println("Guess the button! You have 5 chances.");
while (true) {
char k = keypad.getKey();
if (k) {
if (k == rand_elem) {
Serial.println("Correct! Your score is " + String(++score) + "\n");
break;
}
else {
if (--chances > 0) {
Serial.println("Whoops! You have " + String(chances) + " more chances.");
}
else {
Serial.println("Oh no, you lose! Your score is " + String(score));
while(true) delay(10000);
}
}
}
delay(100);
}
delay(1000);
}