/* 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 "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 #define Password_Lenght 9 //give enough room for 8 chars + NULL char char Data[Password_Lenght]; //8 is the number of chars it can hold + the null char = 9 char Master[Password_Lenght] = "12345678";//default password is 12345678 byte data_count = 0; //variable to store the number counting 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*****"); } void loop() { char customKey = customKeypad.getKey();//reads the key pressed if (customKey) // makes sure that the key is actually pressed { Data[data_count] = customKey; // store char into data array Serial.print(customKey); // print char at said cursor lcd.setCursor(data_count, 1); lcd.print(customKey); data_count++; // increment data array by 1 to store new char, also keep track of the number of chars entered } if (data_count == Password_Lenght - 1) // if the array index is equal to the number of expected chars, compare data to master { if (!strcmp(Data, Master)) { 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);//green LED is on Serial.println(""); Serial.println("********************"); Serial.println("** ACCESS GRANTED **"); Serial.println("** WELCOME!! **"); Serial.println("********************"); delay(2000);//sets delay for 2 seconds digitalWrite(greenledPin, LOW);//green LED is off resetAll ();//reset all function } else { keypadRead = 0; playTune(keypadRead); digitalWrite(redledPin, HIGH);//red LED is on 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);//sets delay for 2 seconds digitalWrite(redledPin, LOW);//red LED is off resetAll ();//reset all function } } } //*********************************************************************** void resetAll () { 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("***Keypad Lock**");//Print at cursor Location Serial.println("********************"); Serial.println("*****Electronic*****"); Serial.println("*****Keypad Lock****"); Serial.println("********************"); delay(2000);//sets delay for 2 seconds 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*****"); while (data_count != 0) { Data[data_count--] = 0; //clear array for new data } return; } //************************************************************** //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); } }