#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Setup for LCD (16x2)
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int wires[4] = {2, 3, 4, 5}; // Pins for the wires
const int buttonPin = 7; // Pin for start button
const int buzzerPin = 6; // Pin for buzzer
const int ledPin = 9; // Pin for the LED (will match buzzer)
const char* wireColors[4] = {"R", "G", "Y", "B"}; // Array of wire color abbreviations
int correctWires[3]; // Stores the randomly selected wires
int timerDuration = 30; // Set timer duration in seconds
int remainingTime;
bool gameActive = false;
bool gameWon = false;
void setup() {
lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
lcd.backlight();
// Setup pins
for (int i = 0; i < 4; i++) {
pinMode(wires[i], INPUT_PULLUP); // Wires as inputs
}
pinMode(buttonPin, INPUT_PULLUP); // Button as input
pinMode(buzzerPin, OUTPUT); // Buzzer as output
pinMode(ledPin, OUTPUT); // LED as output
// Show welcome message
lcd.clear();
lcd.print("Welcome to the");
lcd.setCursor(0, 1); // Move cursor to second row
lcd.print("Defuse Game!");
buzz(1000); // Sound buzzer at start with LED sync
delay(3000); // Delay for 3 seconds to show the message
lcd.clear();
lcd.print("Press Button");
lcd.setCursor(0, 1);
lcd.print("to Start!");
}
void loop() {
if (digitalRead(buttonPin) == LOW && !gameActive) {
startGame();
}
if (gameActive) {
if (remainingTime > 0) {
checkWireCut();
updateTimer();
} else {
loseGame(); // Timer ran out, player loses
}
}
}
void startGame() {
randomSeed(millis());
gameActive = true;
gameWon = false;
remainingTime = timerDuration;
// Randomly select 3 unique wires
for (int i = 0; i < 3; i++) {
correctWires[i] = random(0, 4); // Random number between 0 and 3
for (int j = 0; j < i; j++) {
if (correctWires[i] == correctWires[j]) {
i--; // If duplicate, re-randomize
}
}
}
// Show selected wires to cut on the LCD
lcd.clear();
lcd.print("Cut wires: ");
for (int i = 0; i < 3; i++) {
lcd.print(wireColors[correctWires[i]]); // Show wire color (R, G, Y, B)
if (i < 2) {
lcd.print(", "); // Add comma between wire colors
}
}
delay(4000); // Display the wire colors for 4 seconds
// Show timer at the bottom row of the LCD
lcd.clear();
lcd.print("Cut the Wires!");
lcd.setCursor(0, 1); // Move to second row (bottom) for timer
lcd.print("Time: ");
lcd.print(remainingTime);
buzz(1000); // Sound buzzer at the start with LED sync
}
void checkWireCut() {
for (int i = 0; i < 4; i++) {
if (digitalRead(wires[i]) == HIGH) { // Wire removed
if (isCorrectWire(i)) {
buzz(500); // Correct wire sound with LED sync
lcd.clear();
lcd.print("Wire ");
lcd.print(wireColors[i]);
lcd.print(" cut!");
delay(500);
if (allCorrectWiresCut()) {
winGame();
}
} else {
loseGame(); // Incorrect wire cut
}
}
}
}
bool isCorrectWire(int wire) {
for (int i = 0; i < 3; i++) {
if (correctWires[i] == wire) {
return true;
}
}
return false;
}
bool allCorrectWiresCut() {
for (int i = 0; i < 3; i++) {
if (digitalRead(wires[correctWires[i]]) == LOW) {
return false;
}
}
return true;
}
void updateTimer() {
delay(1000);
remainingTime--;
// Update timer on the second row of the LCD
lcd.setCursor(6, 1); // Move to timer position on the second row
lcd.print(remainingTime);
if (remainingTime <= 0) {
loseGame();
}
}
void winGame() {
gameActive = false;
gameWon = true;
lcd.clear();
lcd.print("You Defused it!");
buzz(2000); // Winning sound with LED sync
delay(5000); // 5-second delay
resetGame();
}
void loseGame() {
gameActive = false;
lcd.clear();
lcd.print("Boom! You Lost!");
buzz(3000); // Losing sound with LED sync
delay(5000); // 5-second delay
resetGame();
}
void resetGame() {
lcd.clear(); // Clear both rows of the LCD
lcd.print("Press Button");
lcd.setCursor(0, 1);
lcd.print("to Start!"); // Ensure the second row is reset as well
}
void buzz(int duration) {
tone(buzzerPin, 1000);
digitalWrite(ledPin, HIGH); // Turn on LED when buzzer sounds
delay(duration);
noTone(buzzerPin);
digitalWrite(ledPin, LOW); // Turn off LED after buzzer
}