#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//#include <pitches.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
const long NOTE_SUSTAIN = 40;
long BUZZ_PIN = 10;
byte i; //starting point for passcode
byte j; //counter
String Passcode;
String CorrectPasscode = "123A45";
unsigned long lastTime = 0;
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] =
{
{ '1','2','3','A' }, { '4','5','6','B' }, { '7','8','9','C' }, { '*','0','#','D' }
};
byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup(){
Serial.begin(9600);
starter();
}
void starter(){
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.setCursor(3,0);
lcd.print("BOMB ARMED");
lcd.setCursor(0,1);
lcd.print("PASSCODE: ______");
i = 10;
j = 0;
}
void loop(){
char customKey = customKeypad.getKey();
if (millis() - lastTime >=500) {
j++;
lastTime = millis();
if (millis() <= 30000 && j >= 4) { //THESE TIMES WILL NEED ADJUSTED
tone(BUZZ_PIN,2093,10);
j=0;
}
else if (millis() >= 30000 && j >= 2) {
tone(BUZZ_PIN,2093,10);
j=0;
}
else if (millis() > 60000){
tone(BUZZ_PIN,2093,10);
j=0;
}
}
if (customKey >= '0'){
lcd.setCursor(i,1);
lcd.print(customKey);
Passcode = Passcode + customKey;
i++;
tone(BUZZ_PIN,1047,10);
//Serial.println(Passcode);
}
else if (customKey == '*'){
i--;
Passcode = Passcode.substring(0, i-10);
lcd.setCursor(i,1);
lcd.print('_');
tone(BUZZ_PIN,523,10);
//Serial.println(Passcode);
}
else if (customKey == '#'){
//check if code is correct
if (Passcode == CorrectPasscode){
lcd.clear();
for(uint8_t nLoop = 0;nLoop < 2;nLoop ++)
{
tone(BUZZ_PIN,880); //a5
delay(NOTE_SUSTAIN);
tone(BUZZ_PIN,988); //b5
delay(NOTE_SUSTAIN);
tone(BUZZ_PIN,523); //c5
delay(NOTE_SUSTAIN);
tone(BUZZ_PIN,988);
delay(NOTE_SUSTAIN);
tone(BUZZ_PIN,523);
delay(NOTE_SUSTAIN);
tone(BUZZ_PIN,587);//d5
delay(NOTE_SUSTAIN);
tone(BUZZ_PIN,523);
delay(NOTE_SUSTAIN);
tone(BUZZ_PIN,587);
delay(NOTE_SUSTAIN);
tone(BUZZ_PIN,659);//e5
delay(NOTE_SUSTAIN);
tone(BUZZ_PIN,587);
delay(NOTE_SUSTAIN);
tone(BUZZ_PIN,659);
delay(NOTE_SUSTAIN);
tone(BUZZ_PIN,659);
delay(NOTE_SUSTAIN);
}
//delay(500);
lcd.setCursor(1,0);
lcd.print("BOMB DISARMED!");
delay(300);
lcd.clear();
delay(300);
lcd.setCursor(1,0);
lcd.print("BOMB DISARMED!");
delay(300);
lcd.clear();
delay(300);
lcd.setCursor(1,0);
lcd.print("BOMB DISARMED!");
noTone(BUZZ_PIN);
BUZZ_PIN = 11;
}
else {
Passcode = "";
lcd.clear();
delay(500);
lcd.setCursor(3,0);
lcd.print("INCORRECT");
lcd.setCursor(3,1);
lcd.print("PASSCODE!");
tone(BUZZ_PIN,392); //g4
delay(250);
tone(BUZZ_PIN,262); //c4
delay(500);
noTone(BUZZ_PIN);
lastTime = millis();
starter();
return;
}
}
// when characters arrive overthe serial port... if (Serial.available()) {
// wait a bit for the entire message to arrive
//delay(100);
// clear the screen
//lcd.clear();
// read all the available characters
//while (Serial.available() > 0) {
//char incomingByte=Serial.read();
// skip the line-feed(ASCII 10) character
//if(incomingByte==10){break;}
// display each characterto the LCD
//lcd.print(incomingByte);
//}
}