/* Project: 4x4 membrane matrix keypad, LEDs, active piezo buzzer, LCD1602 I2C module / security code Function: This sketch takes input from a keypad for secret code. If the code is entered correctly it turns on a green LED to simulate access granted and prints access granted in Serial Monitor and on LCD screen. Otherwise the red LED is lit and access denied is printed in Serial Monitor and on LCD screen. */ // Keypad Library for Arduino // Authors: Mark Stanley, Alexander Brevig // http://playground.arduino.cc/Main/KeypadTutorial //4x4 matrix membrane keypad //****************************************************************************************** #include //include library code #include //include library code #include "pitches.h"//include library code #include //include library code //****************************************************************************************** // Melodies definition: access and rejection - you can change melodies. see in file pitches.h int keypadRead; // keypad read 1 = good, 0 = bad for playTune function int access_melody[] = { NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4}; int access_noteDurations[] = {4,8,8,4,4,4,4,4}; int noaccess_melody[] = {NOTE_G2, NOTE_G2}; int noaccess_noteDurations[] = {8,8}; const int buzzerPin = 12; //active piezo buzzer attached to digital pin 12 LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);//Initialise the LCD 1602 I2C module const int greenledPin = 10; //green LED attached to digital pin 10 const int redledPin = 11; //red LED attached to digital pin 11 Password password = Password( "2468" ); // Set password as 2468 const byte rows = 4; //number of keypad rows const byte cols = 4; //number of keypad columns char keys[rows][cols] = { //enter the setup of your keypad {'1','2','3','A'}, {'4','5','6','B'}, {'7','8','9','C'}, {'*','0','#','D'} }; byte rowPins[rows] = {9, 8, 7, 6}; //ROW1, ROW2, ROW3 and ROW4 pins declared here. Change them to yours byte colPins[cols] = {5, 4, 3, 2}; //COL1, COL2, COL3 and COL4 pins declared here. Change them to yours Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols); int i = 0; //****************************************************************************************** void setup() { pinMode(buzzerPin, OUTPUT); //sets buzzerPin as OUTPUT pinMode(greenledPin, OUTPUT); //sets greenledPin as OUTPUT pinMode(redledPin, OUTPUT); //sets redledPin as OUTPUT Serial.begin(9600); //initialise the serial communication at 9600 bps lcd.begin (16, 2);//Define the LCD as 16 column by 2 rows lcd.setBacklightPin(3, POSITIVE);//Switch on the backlight lcd.setBacklight(HIGH); lcd.print("***Electronic***");//Print at cursor Location lcd.setCursor(0, 1); //goto first column (column 0) and second line (line 1) lcd.print("***Keypad Lock**");//Print at cursor Location Serial.println("********************"); Serial.println("*****Electronic*****"); Serial.println("*****Keypad Lock****"); Serial.println("********************"); delay(2000); lcd.clear(); lcd.print("Enter code + * :"); lcd.setCursor(0, 1); //goto first column (column 0) and second line (line 1) lcd.blink(); Serial.println("Enter code, press * :"); keypad.addEventListener(keypadEvent); // Add an event listener to detect keypresses } void loop() { keypad.getKey(); } //***************************************************************** void keypadEvent(KeypadEvent eKey) { switch (keypad.getState()) { case PRESSED: lcd.setCursor(i, 1); lcd.print(eKey); i++; Serial.print("Pressed key: "); Serial.println(eKey); switch (eKey) { case '*': lcd.clear(); lcd.print("Enter code + * :"); lcd.setCursor(0, 1); //goto first column (column 0) and second line (line 1) lcd.blink(); Serial.println("Enter code, press * :"); checkPassword(); break; case '#': lcd.clear(); lcd.print("Enter code + * :"); lcd.setCursor(0, 1); //goto first column (column 0) and second line (line 1) lcd.blink(); Serial.println("Enter code, press * :"); password.reset(); i = 0; break; default: password.append(eKey); } } } //***************************************************************** void checkPassword() { if (password.evaluate() ) { keypadRead = 1; playTune(keypadRead); digitalWrite(greenledPin, HIGH); // Turn on green LED lcd.clear(); lcd.print("***Electronic***");//Print at cursor Location lcd.setCursor(0, 1); //goto first column (column 0) and second line (line 1) lcd.print("*Access granted*"); Serial.println("");// If the password is correct... Serial.println("********************"); Serial.println("** ACCESS GRANTED **"); Serial.println("** WELCOME!! **"); Serial.println("********************"); delay(2000);//sets delay for 2 seconds lcd.clear(); lcd.print("Press # to reset");//Print at cursor Location Serial.println("Press # to reset"); digitalWrite(greenledPin, LOW); // Turn off green LED } else { keypadRead = 0; playTune(keypadRead); digitalWrite(redledPin, HIGH); // Turn on red LED lcd.clear(); lcd.print("***Electronic***");//Print at cursor Location lcd.setCursor(0, 1); //goto second column (column 2) and second line (line 1) lcd.print("**Access denied*"); Serial.println("");// If the password is incorrect... Serial.println("********************"); Serial.println("** ACCESS DENIED! **"); Serial.println("** INVALID CODE **"); Serial.println("********************"); delay(2000);//sets delay for 2 seconds lcd.clear(); lcd.print("Press # to reset");//Print at cursor Location Serial.println("Press # to reset"); digitalWrite(redledPin, LOW); // Turn off red LED } } //************************************************************** //Function to play the melody during Access and No access void playTune(int Scan) { if (Scan == 1) // Correct secret code entered - Access granted { for (int i = 0; i < 8; i++) //loop through the notes { // Good secret code read int access_noteDuration = 1000 / access_noteDurations[i]; tone(buzzerPin, access_melody[i], access_noteDuration); int access_pauseBetweenNotes = access_noteDuration * 1.30; delay(access_pauseBetweenNotes); noTone(buzzerPin); } } else // Wrong secret code entered - No Access for (int i = 0; i < 2; i++) //loop through the notes { int noaccess_noteDuration = 1000 / noaccess_noteDurations[i]; tone(buzzerPin, noaccess_melody[i], noaccess_noteDuration); int noaccess_pauseBetweenNotes = noaccess_noteDuration * 1.30; delay(noaccess_pauseBetweenNotes); noTone(buzzerPin); } }