// Marica Mihai // 332CA #include #include // LCD DISPLAY LiquidCrystal_I2C lcd(0x27, 16, 2); /* * */ // KEY-PAD const byte ROWS = 4; // four rows const byte COLS = 3; // four columns char hexaKeys[ROWS][COLS] = { {'1','2','3'}, {'4','5','6'}, {'7','8','9'}, {'*','0','#'} }; byte rowPins[ROWS] = {2, 3, 4, 5}; // connect to the row pinouts of the keypad byte colPins[COLS] = {6, 7, 8}; // connect to the column pinouts of the keypad Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); /* . . . . . . */ const int buzzer = 11; // buzzer to arduino pin 11 const int red_led = 9; // red led to arduino pin 9 const int green_led = 10; // green led to arduino pin 10 const int refreshTime = 1000; unsigned long lastTime = 0; unsigned long timeLeft = 60; int ledRefreshTime; unsigned long ledLastTime = 0; int digitsWritten = 0; char password[4]; char guess[4]; int hint1 = 0; int hint2 = 0; int hint1poz, hint2poz; void setup() { // put your setup code here, to run once: pinMode(red_led, OUTPUT); // set red led - pin 9 as an output pinMode(green_led, OUTPUT); lcd.begin(); lcd.backlight(); lcd.print("Bomb has been"); lcd.setCursor(0, 1); lcd.print("planted!"); delay(2000); randomSeed(analogRead(0)); for (int i = 0; i < 4; i++) { int random_nr = random(48, 58); password[i] = random_nr; } hint1poz = random(0, 4); while(hint2poz == hint1poz) { hint2poz = random(0, 4); } lcd.clear(); lcd.setCursor(0,0); lcd.print(password); delay(2000); lcd.setCursor(0,0); lcd.print("Time left:"); lcd.setCursor(0,1); lcd.print("---- Hint:----"); lastTime = millis(); } void gameLost() { lcd.clear(); for (int i = 0; i < 3; i++) { digitalWrite(red_led, HIGH); tone(buzzer, 2000); delay(500); digitalWrite(red_led, LOW); noTone(buzzer); delay(500); } delay(500); lcd.print("Bomb exploded!"); lcd.setCursor(0, 1); lcd.print("You lost!"); digitalWrite(red_led, HIGH); tone(buzzer, 1000); while(1) { delay(500); } } void gameWon() { lcd.clear(); lcd.print("Bomb diffused!"); lcd.setCursor(0, 1); lcd.print("You win!"); digitalWrite(green_led, HIGH); tone(buzzer, 4000); delay(1000); noTone(buzzer); while(1) { delay(500); } } int passwordMatches() { for (int i = 0 ; i < 4; i++) { if (password[i] != guess[i]) return 0; } return 1; } void loop() { if(millis() - lastTime >= refreshTime) { lastTime += refreshTime; lcd.setCursor(10, 0); lcd.print(timeLeft); lcd.print(" "); timeLeft--; if (timeLeft == 0) { gameLost(); } } ledRefreshTime = 1000 * timeLeft / 60 + 200; if(millis() - ledLastTime >= ledRefreshTime) { ledLastTime += ledRefreshTime; digitalWrite(red_led, HIGH); delay(100); digitalWrite(red_led, LOW); } if (timeLeft <= 39) { hint1 = 1; lcd.setCursor(10 + hint1poz, 1); lcd.print(password[hint1poz]); } if (timeLeft <= 19) { hint2 = 1; lcd.setCursor(10 + hint2poz, 1); lcd.print(password[hint2poz]); } char customKey = customKeypad.getKey(); if (customKey){ guess[digitsWritten] = customKey; lcd.setCursor(digitsWritten, 1); lcd.print(customKey); digitsWritten++; if (digitsWritten == 4) { if (passwordMatches() == 1) { delay(500); gameWon(); } delay(300); lcd.setCursor(0, 1); lcd.print("Wrong password"); tone(buzzer, 500); delay(500); noTone(buzzer); lcd.setCursor(0, 1); lcd.print("---- Hint:----"); digitsWritten = 0; } } }