#include <Keypad.h>
const int ROW_NUM = 4;
const int COL_NUM = 4;
const int MAX_SEQUENCE_LENGTH = 16; // Максимальная длина последовательности
char keys[ROW_NUM][COL_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6};
byte pin_cols[COL_NUM] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_cols, ROW_NUM, COL_NUM);
char sequence[MAX_SEQUENCE_LENGTH];
float keyHoldTimes[MAX_SEQUENCE_LENGTH];
unsigned long startTime[MAX_SEQUENCE_LENGTH];
bool keyPressed[MAX_SEQUENCE_LENGTH];
float holdDuration[MAX_SEQUENCE_LENGTH];
int currentSequenceLength = 3;
int currentIndex = 0;
int score = 0;
void setup() {
Serial.begin(9600);
randomSeed(analogRead(0));
initializeArrays();
generateSequence();
Serial.println("Игра началась");
displaySequence();
}
void loop() {
char key = keypad.getKey();
if (key) {
checkKey(key);
}
if (currentIndex == currentSequenceLength) {
Serial.println("Уровень завершён! Генерация нового");
score++;
currentSequenceLength = min(currentSequenceLength + 1, MAX_SEQUENCE_LENGTH);
initializeArrays();
currentIndex = 0;
generateSequence();
displaySequence();
displayStatistics();
}
}
void initializeArrays() {
for (int i = 0; i < MAX_SEQUENCE_LENGTH; i++) {
keyHoldTimes[i] = 2.0;
holdDuration[i] = 0.0;
keyPressed[i] = false;
startTime[i] = 0;
}
}
bool isUsed(char* usedKeys, int count, char key) {
for (int i = 0; i < count; i++) {
if (usedKeys[i] == key) return true;
}
return false;
}
void generateSequence() {
char usedKeys[MAX_SEQUENCE_LENGTH] = {0};
int usedCount = 0;
for (int i = 0; i < currentSequenceLength; i++) {
char newKey;
do {
int randomIndex = random(0, 16);
newKey = keys[randomIndex / COL_NUM][randomIndex % COL_NUM];
} while (isUsed(usedKeys, usedCount, newKey));
sequence[i] = newKey;
usedKeys[usedCount++] = newKey;
}
}
void displaySequence() {
Serial.println("Последовательность для повторения:");
for (int i = 0; i < currentSequenceLength; i++) {
Serial.print(sequence[i]);
Serial.print(" ");
delay(1500);
}
Serial.println();
Serial.println("повторите последовательность, удерживая кнопки.");
}
void checkKey(char key) {
if (key == sequence[currentIndex]) {
if (!keyPressed[currentIndex]) {
startTime[currentIndex] = millis();
keyPressed[currentIndex] = true;
} else {
unsigned long elapsedTime = millis() - startTime[currentIndex];
if (elapsedTime >= keyHoldTimes[currentIndex] * 1000 - 200 &&
elapsedTime <= keyHoldTimes[currentIndex] * 1000 + 200) {
holdDuration[currentIndex] = elapsedTime / 1000.0;
Serial.print("Правильное время для '");
Serial.print(key);
Serial.print("': ");
Serial.println(holdDuration[currentIndex]);
currentIndex++;
keyPressed[currentIndex - 1] = false;
} else if (elapsedTime > keyHoldTimes[currentIndex] * 1000 + 200) {
Serial.println("Слишком долго! Игра окончена.");
gameOver();
}
}
} else {
Serial.println("Неправильная кнопка! Игра окончена.");
gameOver();
}
}
void displayStatistics() {
Serial.println("Статистика для этого уровня:");
for (int i = 0; i < currentSequenceLength; i++) {
Serial.print("Кнопка '");
Serial.print(sequence[i]);
Serial.print("' удерживалась ");
Serial.print(holdDuration[i], 2);
Serial.println(" секунд.");
}
Serial.println();
}
void gameOver() {
Serial.println("Игра окончена.");
Serial.print("Ваш финальный счёт: ");
Serial.println(score);
while (true);
}