#include <LiquidCrystal.h>
// Define button pins
const int buttonPin1 = 2;
const int buttonPin2 = 3;
// Define LED pins
const int ledPin1 = 4;
const int ledPin2 = 5;
const int ledPin3 = 6;
const int ledPin4 = 7;
// Define game parameters
const int stackHeight = 4; // Number of LEDs representing stack height
int currentHeight = 0; // Current stack height
bool gameRunning = false; // Flag to indicate if the game is running
// Define LCD parameters
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
void setup() {
// Initialize LCD
lcd.begin(16, 2);
// Initialize button pins
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
// Initialize LED pins
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
// Clear LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press Start");
}
void loop() {
// Check if game is not running and start button is pressed
if (!gameRunning && digitalRead(buttonPin1) == LOW) {
startGame();
}
// Check if game is running
if (gameRunning) {
// Check if stack button is pressed
if (digitalRead(buttonPin2) == LOW) {
addBlock();
}
}
}
// Function to start the game
void startGame() {
gameRunning = true;
currentHeight = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Game Started");
updateDisplay();
}
// Function to add a block to the stack
void addBlock() {
if (currentHeight < stackHeight) {
currentHeight++;
updateDisplay();
} else {
endGame();
}
}
// Function to update the display
void updateDisplay() {
// Update LEDs based on currentHeight
digitalWrite(ledPin1, currentHeight >= 1 ? HIGH : LOW);
digitalWrite(ledPin2, currentHeight >= 2 ? HIGH : LOW);
digitalWrite(ledPin3, currentHeight >= 3 ? HIGH : LOW);
digitalWrite(ledPin4, currentHeight >= 4 ? HIGH : LOW);
// Update LCD
lcd.setCursor(0, 1);
lcd.print("Height: ");
lcd.print(currentHeight);
}
// Function to end the game
void endGame() {
gameRunning = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Game Over");
delay(2000); // Delay for 2 seconds
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press Start");
}