#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd1(0x27, 16, 2); // Address 1
LiquidCrystal_I2C lcd2(0x26, 16, 2); // Address 2
const int ledPins[] = {2, 3, 4, 5, 6, 9};
const int buttonPins[] = {A0, A1, A2, A3, A4, A5};
//const int startButtonPin = 10; // Pin for the start button
//const int buzzerPin = 11; // Pin for the buzzer
const int numLeds = sizeof(ledPins) / sizeof(ledPins[0]);
const int numButtons = sizeof(buttonPins) / sizeof(buttonPins[0]);
unsigned long startTime;
unsigned long elapsedTime;
int score;
bool gameRunning = false;
void setup() {
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
}
/* for (int i = 0; i < numButtons; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(startButtonPin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);*/
lcd1.init();
lcd1.backlight();
lcd2.init();
lcd2.backlight();
lcd1.setCursor(0, 0);
lcd1.print(" Reflex Test Game");
lcd2.setCursor(0, 1);
lcd2.print(" Press Start!");
// Initialize other variables
score = 0;
gameRunning = false;
randomSeed(analogRead(0)); // Seed the random number generator
}
void loop() {
if (!gameRunning) {
// Wait for player to press start button
if (digitalRead(startButtonPin) == LOW) {
lcd1.clear();
lcd2.clear();
lcd1.setCursor(0, 0);
lcd1.print("Game Started!");
lcd2.setCursor(0, 1);
lcd2.print("Score: ");
lcd2.print(score);
delay(1000); // Delay for a second
startGame();
}
} else {
playGame();
}
}
void startGame() {
lcd1.clear();
lcd2.clear();
score = 0;
lcd2.setCursor(0, 1);
lcd2.print("Score: ");
lcd2.print(score);
delay(1000); // Delay for a second before starting
startTime = millis();
gameRunning = true;
}
void playGame() {
unsigned long currentTime = millis();
elapsedTime = currentTime - startTime;
// Check if 30 seconds have passed
if (elapsedTime >= 30000) {
endGame();
} else {
int randomLed = random(numLeds);
lightUpLed(randomLed);
int buttonPressed = checkButtonPress();
if (buttonPressed == randomLed) {
score++;
lcd2.setCursor(0, 1);
lcd2.print("Score: ");
lcd2.print(score);
delay(500); // Delay for a moment to show feedback
}
}
}
void endGame() {
lcd1.clear();
lcd2.clear();
lcd1.setCursor(0, 0);
lcd1.print("Game Over!");
lcd2.setCursor(0, 1);
lcd2.print("Final Score: ");
lcd2.print(score);
gameRunning = false;
}
void lightUpLed(int ledIndex) {
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], LOW);
}
digitalWrite(ledPins[ledIndex], HIGH);
}
int checkButtonPress() {
for (int i = 0; i < numButtons; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
return i;
}
}
return -1; // No button pressed
}