/* Project: 4x4 rigid 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. */ //********************************************************************************** #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 char password[8]; char pass[8], pass1[8]; int i = 0; char customKey = 0; 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] = {5, 4, 3, 2}; //ROW1, ROW2, ROW3 and ROW4 pins declared here. Change them to yours byte colPins[cols] = {6, 7, 8, 9}; //COL1, COL2, COL3 and COL4 pins declared here. Change them to yours Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols); //************************************************************************************************ 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*****"); for (int j = 0; j < 8; j++) //password set to 8 symbols: default is 12345678 EEPROM.write(j, j + 49); //if you want to have a different password lengh - change number 8 everywere in void loop() to your number for (int j = 0; j < 8; j++) pass[j] = EEPROM.read(j); } void loop() { customKey = customKeypad.getKey(); if (customKey == '#') change(); if (customKey) { password[i++] = customKey; lcd.setCursor(i - 1, 1); lcd.print(customKey); Serial.print(customKey); } if (i == 8) { delay(200); for (int j = 0; j < 8; j++) pass[j] = EEPROM.read(j); if (!(strncmp(password, pass, 8))) { 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*"); keypadRead = 1; playTune(keypadRead); digitalWrite(greenledPin, HIGH); Serial.println(""); Serial.println("********************"); Serial.println("** ACCESS GRANTED **"); Serial.println("** WELCOME!! **"); Serial.println("********************"); delay(2000); lcd.clear(); lcd.print("Use # to change code");//Print at cursor Location Serial.println("Use # to change code"); 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("********************"); Serial.println("*****Enter code*****"); i = 0; digitalWrite(greenledPin, LOW); } else { keypadRead = 0; playTune(keypadRead); digitalWrite(redledPin, HIGH); 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(""); Serial.println("********************"); Serial.println("** ACCESS DENIED! **"); Serial.println("** INVALID CODE **"); Serial.println("********************"); delay(2000); lcd.clear(); lcd.print("Use # to change code");//Print at cursor Location Serial.println("Use # to change code"); 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("********************"); Serial.println("*****Enter code*****"); i = 0; digitalWrite(redledPin, LOW); } } } void change() { int j = 0; lcd.clear(); lcd.print("Enter old code:"); lcd.setCursor(0, 1); //goto first column (column 0) and second line (line 1) lcd.blink(); Serial.println("*Enter current code*"); while (j < 8) { char key = customKeypad.getKey(); if (key) { pass1[j++] = key; lcd.setCursor(j-1, 1); lcd.print(customKey); Serial.print(key); } key = 0; } delay(500); if ((strncmp(pass1, pass, 8))) { keypadRead = 0; playTune(keypadRead); digitalWrite(redledPin, HIGH); 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(""); Serial.println("********************"); Serial.println("** ACCESS DENIED! **"); Serial.println("** INVALID CODE **"); Serial.println("********************"); delay(1000); } else { j = 0; lcd.clear(); lcd.print("Enter new code:"); lcd.setCursor(0, 1); //goto first column (column 0) and second line (line 1) lcd.blink(); Serial.println(""); Serial.println("***Enter new code***"); while (j < 8) { char key = customKeypad.getKey(); if (key) { pass[j] = key; lcd.setCursor(j, 1); lcd.print(customKey); Serial.print(key); EEPROM.write(j, key); j++; } } lcd.clear(); lcd.print("Code changed");//Print at cursor Location Serial.println(""); Serial.println("**Your code changed*"); 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*****"); customKey = 0; } //************************************************************** //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); } }