#include <LiquidCrystal_I2C.h>
#define LCD_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_ROWS 2
LiquidCrystal_I2C lcd(LCD_ADDR, LCD_COLUMNS, LCD_ROWS);
const int buttonPins[] = {8, 9, 10, 11};
const int ledPins[] = {2, 3, 4, 5};
int currentLED;
float startTime;
float timeTaken;
int score=0;
void setup() {
lcd.init();
lcd.backlight();
for (int i = 0; i < 4; i++)
pinMode(buttonPins[i], INPUT_PULLUP);
for (int i = 0; i < 4; i++)
pinMode(ledPins[i], OUTPUT);
randomSeed(analogRead(0)); // Seed the random number generator
}
void loop() {
currentLED = random(0, 4);
digitalWrite(ledPins[currentLED], HIGH); // Turn on the selected LED
startTime = millis();
while (true) {
// Check if any button is pressed
for (int i = 0; i < 4; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
timeTaken = millis() - startTime;
digitalWrite(ledPins[currentLED], LOW);
lcd.clear(); // Clear the LCD screen
lcd.setCursor(0, 0);
lcd.print("Time taken: ");
lcd.print(timeTaken / 1000);
lcd.print("s");
lcd.print("score: ");
if (i == currentLED){
lcd.setCursor(0, 1);
score++;
lcd.print("score:");
lcd.print(score);
}
else
{
lcd.setCursor(0, 1);
lcd.print("game over");
score=0;
}
delay(700);
return;
}
}
}
}