#define TRIG_PIN 9
#define ECHO_PIN 10
#define BUZZER_PIN 11
#define LED_PIN 12
int sequence[] = {20, 10, 30, 20};
String directions[] = {"до нужного числа", "влево", "вправо", "влево"};
int currentStep = 0;
unsigned long stepStartTime;
bool gameActive = false;
const unsigned long TIME_LIMIT = 5000;
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
startGame();
}
void loop() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
float distance = duration * 0.035 / 2;
if (gameActive) {
if (distance >= sequence[currentStep] - 1 && distance <= sequence[currentStep] + 1) {
unsigned long reactionTime = millis() - stepStartTime;
Serial.print("Этап ");
Serial.print(currentStep + 1);
Serial.print(" пройдено! Дистанция: ");
Serial.print(sequence[currentStep]);
Serial.print(" см. Время реакций: ");
Serial.print(reactionTime);
Serial.println(" МС.");
currentStep++;
if (currentStep >= sizeof(sequence) / sizeof(sequence[0])) {
Serial.println("Поздравляю! Ты прошел этап.");
digitalWrite(LED_PIN, HIGH);
tone(BUZZER_PIN, 1000, 2000);
delay(2000);
digitalWrite(LED_PIN, LOW);
gameActive = false;
} else {
stepStartTime = millis();
Serial.print("Следующий этап: пройди дистанцию ");
Serial.print(sequence[currentStep]);
Serial.print(" см. Двигайся ");
Serial.print(directions[currentStep]);
Serial.println(".");
}
}
if (millis() - stepStartTime > TIME_LIMIT) {
Serial.println("РЕСТАРТ.");
resetGame();
}
}
delay(100);
}
void startGame() {
Serial.println("ИГРА НАЧАЛАСЬ.");
currentStep = 0;
stepStartTime = millis();
gameActive = true;
Serial.print("Первый этап: пройди дистанцию ");
Serial.print(sequence[currentStep]);
Serial.print(" см. Двигайся ");
Serial.print(directions[currentStep]);
Serial.println(".");
}
void resetGame() {
for (int i = 0; i < 3; i++) {
tone(BUZZER_PIN, 500, 500);
digitalWrite(LED_PIN, HIGH);
delay(500);
noTone(BUZZER_PIN);
digitalWrite(LED_PIN, LOW);
delay(500);
}
Serial.println("Игра сброшена! Начнем сначала...");
startGame();
}