#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Keypad.h>
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {22,23,25,27};
byte colPins[COLS] = {29,31,33,35};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
struct NumberPosition {
int x, y, value;
};
NumberPosition numbers[7];
void setup() {
Serial.begin(9600);
tft.begin();
tft.fillScreen(ILI9341_BLACK);
randomSeed(analogRead(0));
for (int i = 0; i < 7; i++) {
numbers[i].value = random(0, 10);
numbers[i].x = random(20, 200);
numbers[i].y = random(20, 280);
}
// Show numbers at the start for memorization
for (int i = 0; i < 7; i++) {
tft.setCursor(numbers[i].x, numbers[i].y);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print(numbers[i].value);
}
delay(3000); // Give more time for memorization
tft.fillScreen(ILI9341_BLACK);
for (int i = 0; i < 7; i++) {
tft.fillRect(numbers[i].x, numbers[i].y, 20, 20, ILI9341_BLUE);
char key = ' ';
while (key == ' ') {
key = keypad.getKey();
}
if (key - '0' == numbers[i].value) {
tft.fillRect(numbers[i].x, numbers[i].y, 20, 20, ILI9341_GREEN);
} else {
tft.fillRect(numbers[i].x, numbers[i].y, 20, 20, ILI9341_RED);
while (1); // Stop execution on wrong guess
}
delay(1000);
}
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(50, 150);
tft.setTextSize(2);
tft.setTextColor(ILI9341_GREEN);
tft.print("You won!");
}
void loop() { }