#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <IRremote.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define the pin for the IR receiver
const int RECV_PIN = 11;
// Define the LED pins
const int redLedPin = 10;
const int greenLedPin = 9;
// Define the 7-segment display pins
const int segA = 2;
const int segB = 3;
const int segC = 4;
const int segD = 5;
const int segE = 6;
const int segF = 7;
const int segG = 8;
// Define the sequence of buttons to be pressed
unsigned long buttonSequence[] = {0xFF30CF, 0xFF30CF, 0xFF18E7}; // button “1,1,2” (You can change the password by changing the hex code here)
int buttonIndex = 0;
// Define the countdown time
int countdown = 9;
// Set up the IR receiver
IRrecv irrecv(RECV_PIN);
decode_results results;
// Variable to track the game state
bool gameOn = false;
// Function to turn off all segments of the 7-segment display
void clearDisplay() {
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
}
// Function to display numbers 0-9 on the 7-segment display
void displayNumber(int num) {
clearDisplay();
switch (num) {
case 0:
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
break;
case 1:
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
break;
case 2:
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segG, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segD, HIGH);
break;
case 3:
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segG, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
break;
case 4:
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
break;
case 5:
digitalWrite(segA, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
break;
case 6:
digitalWrite(segA, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
break;
case 7:
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
break;
case 8:
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;
case 9:
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;
}
}
void setup() {
// Initialize the serial communication
Serial.begin(9600);
// Initialize the LCD
lcd.init();
lcd.backlight();
// Set the LED pins as output
pinMode(redLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
// Set the 7-segment display pins as output
pinMode(segA, OUTPUT);
pinMode(segB, OUTPUT);
pinMode(segC, OUTPUT);
pinMode(segD, OUTPUT);
pinMode(segE, OUTPUT);
pinMode(segF, OUTPUT);
pinMode(segG, OUTPUT);
// Set up the IR receiver
irrecv.enableIRIn();
gameOn = false;
}
void loop() {
START:
if (!gameOn) {
// Turn on the game
gameOn = true;
// Add any necessary code to start the game’s functionality
lcd.clear(); // Clear the LCD
lcd.print(“Game On”); // Print the game on message
delay(300);
}
// If there are any IR commands, decode them
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX); // Print the received IR code in hexadecimal
// If the button pressed is the next in the sequence
if (results.value == buttonSequence[buttonIndex] || buttonIndex == sizeof(buttonSequence) / sizeof(buttonSequence[0])) {
buttonIndex++; // Move to the next button in the sequence
if (buttonIndex == sizeof(buttonSequence) / sizeof(buttonSequence[0])) { // If the entire sequence was pressed
digitalWrite(greenLedPin, HIGH); // Turn on the green LED
lcd.clear(); // Clear the LCD
lcd.print(“Success!”); // Print the success message
delay(1000); // Wait for 1 second
digitalWrite(greenLedPin, LOW); // Turn off the green LED
lcd.clear();
lcd.print(“Restarting…”); // Print the success message
for (int i = 0; i < 11; i++) {
delay(100); // Wait for 1 second
// toggle LEDs quickly
digitalWrite(greenLedPin, i & 1);
digitalWrite(redLedPin, i & 1);
}
lcd.clear(); // Clear the LCD
buttonIndex = 0; // Reset the button index for the next round
gameOn = false;
countdown = 9;
}
} else { // If the button pressed is not in the sequence
digitalWrite(redLedPin, HIGH); // Turn on the red LED
lcd.clear(); // Clear the LCD
lcd.print(“Try Again!”); // Print the failure message
delay(1000); // Wait for 1 second
digitalWrite(redLedPin, LOW); // Turn off the red LED
lcd.clear(); // Clear the LCD
buttonIndex = 0; // Reset the button index for the next round
}
irrecv.resume(); // Receive the next value
}
// Update the countdown timer
if (countdown >= 0) {
displayNumber(countdown); // Display the current countdown value on the 7-segment display
countdown–; // Decrement the countdown after displaying
} else {
digitalWrite(redLedPin, HIGH); // Turn on the red LED
lcd.clear(); // Clear the LCD
lcd.print(“Time’s Up!”); // Print the failure message
delay(1000); // Wait for 1 second
digitalWrite(redLedPin, LOW); // Turn off the red LED
lcd.clear(); // Clear the LCD
countdown = 9; // Reset the countdown for the next round
buttonIndex = 0; // Reset the button index for the next round
}
delay(1000); // Wait for 1 second
}