#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define the I2C address for the LCD
#define I2C_ADDR 0x27
// Define LCD dimensions
#define LCD_COLS 16
#define LCD_ROWS 2
// Initialize the LCD with the I2C address
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLS, LCD_ROWS);
// Define pushbutton pin
#define BUTTON_PIN 2
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
// Initialize pushbutton pin
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Print initial message on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press button to");
lcd.setCursor(0, 1);
lcd.print("start the game!");
}
void loop() {
// Check if pushbutton is pressed
if (digitalRead(BUTTON_PIN) == LOW) {
// Button pressed, start the game
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Game Started!");
// Your game logic goes here...
// For example, simulate a game by displaying a countdown
for (int i = 3; i > 0; i--) {
lcd.setCursor(0, 1);
lcd.print("Countdown: ");
lcd.print(i);
delay(1000);
}
// Game over message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Game Over!");
delay(2000); // Delay for visibility
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press button to");
lcd.setCursor(0, 1);
lcd.print("restart game!");
// Wait until button is released
while (digitalRead(BUTTON_PIN) == LOW) {
delay(100); // Debouncing
}
}
}