#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
// Define pins
#define START_BUTTON 6
#define LUCK_BUTTON 5
#define SERVO_PIN 3
// LED Pins
const int LEDS[] = {7, 8, 9, 10, 11, 12, 13};
const int NUM_LEDS = 7;
// LCD Initialization (address: 0x27)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Servo Initialization
Servo prizeServo;
// Game Variables
int currentLED = 0;
bool direction = true;
int wins = 0;
int attempts = 0;
bool gameActive = false;
void setup() {
pinMode(START_BUTTON, INPUT_PULLUP);
pinMode(LUCK_BUTTON, INPUT_PULLUP);
for (int i = 0; i < NUM_LEDS; i++) {
pinMode(LEDS[i], OUTPUT);
}
prizeServo.attach(SERVO_PIN);
prizeServo.write(0); // Initial position
lcd.init();
lcd.backlight();
resetGame();
}
void loop() {
if (digitalRead(START_BUTTON) == LOW) {
startGame();
}
if (gameActive) {
runLEDSequence();
if (digitalRead(LUCK_BUTTON) == LOW) {
checkWin();
}
}
}
void startGame() {
gameActive = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Game Started!");
delay(1000);
}
void runLEDSequence() {
digitalWrite(LEDS[currentLED], LOW);
if (direction) {
currentLED++;
if (currentLED == NUM_LEDS - 1) direction = false;
} else {
currentLED--;
if (currentLED == 0) direction = true;
}
digitalWrite(LEDS[currentLED], HIGH);
delay(200);
}
void checkWin() {
if (currentLED == 3) { // Check if 4th LED (index 3) is ON
wins++;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("You Win!");
} else {
attempts++;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("You Lose!");
}
lcd.setCursor(0, 1);
lcd.print("Wins: ");
lcd.print(wins);
lcd.print(" Att: ");
lcd.print(attempts);
delay(2000);
resetRound();
}
void resetRound() {
if (wins == 3) {
activatePrize();
resetGame();
} else if (attempts == 3) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Game Over!");
delay(3000);
resetGame();
}
}
void activatePrize() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Prize Unlocked!");
prizeServo.write(90);
delay(5000);
prizeServo.write(0);
}
void resetGame() {
wins = 0;
attempts = 0;
gameActive = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press Start");
delay(500);
}