#include #include #include #include #include #include #define OLED_SCREEN_WIDTH 128 #define OLED_SCREEN_HEIGHT 64 #define LED_PIN 6 // the pin that the LED strip is attached to #define NUM_LEDS 8 // the number of LEDs in the strip // Initialize the OLED screen Adafruit_SSD1306 screen(OLED_SCREEN_WIDTH, OLED_SCREEN_HEIGHT, &Wire, -1); Adafruit_SSD1306 screen1(OLED_SCREEN_WIDTH, OLED_SCREEN_HEIGHT, &Wire, -1); //Initialize the LED strip Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800); // Initialize the keypad const byte ROWS = 4; const byte COLS = 4; char keys[ROWS][COLS] = { {'C','9','1','5'}, {'D','0','2','6'}, {'E','A','3','7'}, {'F','B','4','8'} }; byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the Rows of the keypad pin 8, 7, 6, 5 respectively byte colPins[COLS] = {1, 0, A5, A6}; //connect to the Columns of the keypad pin 4, 3, 2, 1 respectively Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); // create servo object to control a servo Servo myservo; // Initialize the countdown counter unsigned long startTime = 0; unsigned long duration = 0; unsigned long explotionDuration = 60*1000; bool countdownStarted = false; const unsigned long COUNTDOWN_LENGTH = 60 * 1000; // 60 seconds // Initialize the password const int PASSWORD_LENGTH = 4; String password = ""; bool settingPassword = false; bool finish = false; bool correct = false; bool loser = false; bool win = false; int pos = 0; int buzzerPin = 7; // the pin that the buzzer is attached to bool shouldPlay = false; // boolean variable to keep track of whether to play the sound effect String enteredPassword = ""; void setup() { // initialize the LED strip strip.begin(); // turn off all LEDs strip.show(); // attaches the servo on pin 9 to the servo object myservo.attach(9); // Initialize the screen screen.begin(SSD1306_SWITCHCAPVCC, 0x3C); screen.clearDisplay(); screen.setTextColor(WHITE); screen.setTextSize(1); screen.setCursor(5, 25); screen.println("Press A set password"); screen.println(" followed by B"); screen.display(); // Initialize the screen screen1.begin(SSD1306_SWITCHCAPVCC, 0x3D); screen1.clearDisplay(); screen1.setTextColor(WHITE); screen1.setTextSize(2); screen1.setTextWrap(false); screen1.setCursor(19, 19); screen1.println("Explosive"); screen1.println("Escape Game"); screen1.display(); // Initialize the serial port Serial.begin(9600); } void loop() { if(!win && !loser) { strip.setBrightness(50); for (int i = 0; i < NUM_LEDS; i++) { strip.setPixelColor(i, strip.Color(0, 0, 255)); // set the LED color to red } strip.show(); // update the LED strip } // Check if a key is pressed char key = keypad.getKey(); if (key == 'A' && !settingPassword && !countdownStarted) { // Start setting the password settingPassword = true; password = ""; enteredPassword = ""; screen.clearDisplay(); screen.setCursor(10, 20); screen.print("Enter new password:"); screen.println(password); screen.display(); } else if (key == 'B' && settingPassword && !countdownStarted) { // Finish setting the password if (password.length() == PASSWORD_LENGTH) { settingPassword = false; finish = true; screen.clearDisplay(); screen.display(); } } else if (key == 'F' && finish && countdownStarted && (enteredPassword.length() == PASSWORD_LENGTH)) { // Check the entered password if (enteredPassword == password) { screen.clearDisplay(); correct = true; } else { correct = false; screen.clearDisplay(); screen.setCursor(10, 20); screen.println("Password incorrect!"); screen.display(); delay(1000); screen.clearDisplay(); screen.setCursor(10, 20); screen.println("Enter password:"); enteredPassword = ""; screen.display(); } } else if (!correct && settingPassword && password.length() <= PASSWORD_LENGTH) { // Add the entered digit to the password if (key == 'C' && password.length() > 0) { // Remove last character if backspace is pressed password = password.substring(0, password.length()-1); } else if(password.length() < PASSWORD_LENGTH && isDigit(key)) { password += key; } screen.clearDisplay(); screen.setCursor(10, 20); screen.println("Enter new password:"); screen.println(password); screen.display(); } // Update the countdown display if(key =='D' && !countdownStarted && finish) { // Start the countdown startTime = millis(); duration = COUNTDOWN_LENGTH; countdownStarted = true; } if (countdownStarted) { if (!settingPassword && enteredPassword.length() <= PASSWORD_LENGTH && !loser) { // Add the entered digit to the password if (key == 'C' && enteredPassword.length() > 0) { // Remove last character if backspace is pressed enteredPassword = enteredPassword.substring(0, enteredPassword.length()-1); } else if(enteredPassword.length() < PASSWORD_LENGTH && isDigit(key)) { enteredPassword += key; } screen.clearDisplay(); screen.setCursor(10, 20); screen.println("Enter password: "); screen.println(enteredPassword); screen.display(); } unsigned long elapsedTime = millis() - startTime; if (elapsedTime >= duration && !correct) { strip.setBrightness(50); for (int i = 0; i < NUM_LEDS; i++) { strip.setPixelColor(i, strip.Color(255, 0, 0)); // set the LED color to red } strip.show(); // update the LED strip countdownStarted = false; loser = true; screen1.clearDisplay(); screen1.setTextSize(2.8); screen1.setCursor(20, 25); screen1.println("Loser :)"); screen1.display(); screen.clearDisplay(); screen.setTextSize(2); screen.setCursor(10, 25); screen.print("Game over"); screen.display(); digitalWrite(7, HIGH); // turn the LED on (HIGH is the voltage level) delay(100); // wait for a second digitalWrite(7, LOW); // turn the LED off by making the voltage LOW delay(50); // wait for a second } else if (elapsedTime < duration && correct) { win = true; countdownStarted = false; screen1.clearDisplay(); screen1.setTextSize(2.8); screen1.setCursor(20, 25); screen1.println("You win!"); screen1.display(); screen.clearDisplay(); screen.setTextSize(2.8); screen.setCursor(20, 25); screen.println("hehehe!!"); screen.display(); strip.setBrightness(50); for (int i = 0; i < NUM_LEDS; i++) { strip.setPixelColor(i, strip.Color(0, 255, 0)); // set the LED color to red } strip.show(); // update the LED strip } else { unsigned long remainingTime = duration - elapsedTime; unsigned long seconds = remainingTime / 1000; unsigned long minutes = seconds / 60; seconds = seconds % 60; screen1.clearDisplay(); screen1.setTextSize(3.5); screen1.setCursor(20, 25); screen1.print(minutes, DEC); screen1.print(":"); if (seconds < 10) { screen1.print("0"); } screen1.print(seconds, DEC); screen1.display(); } } }