Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This sample code is Solenoid Door Lock Using Keypad Tutorial.
/*
Project: CUnlock the Soenoid Door Lock Using A Keypad and Maker Nano #ArduinoNano
Board: Arduino Nano (Maker Nano)
Connections:
Nano | Relay
GND – GND
5V – VCC
A0 – SIG
Nano | Keypad
11 – pin 1 (most left)
10 – pin 2
9 – pin 3
7 – pin 4
6 – pin 5
5 – pin 6
4 – pin 7
3 – pin 8
Nano | I2C LCD
GND – GND
5V – VCC
SDL – SDL
SCA - SCA
External libraries:
– Keypad by Mark Stanley V3.1.1 (Manager)
*/
#include <Keypad.h>
#define Password_Lenght 5 // Give enough room for 4 chars + NULL char
#define PIEZO 8
#define RELAY A0
#define NOTE_G5 784
#define NOTE_C6 1047
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 2);
int pos = 0; // variable to store the servo position
char Data[Password_Lenght];
char Master[Password_Lenght] = "5698";
byte data_count = 0, master_count = 0;
char customKey;
const byte ROWS = 4;
const byte COLS = 4;
char keys [ROWS] [COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {6, 5, 4, 3};
byte colPins[COLS] = {11, 10, 9, 7};
Keypad myKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int OpenMelody[] = {NOTE_G5, NOTE_C6};
int OpenNoteDurations[] = {12, 8};
int CloseMelody[] = {NOTE_C6, NOTE_G5};
int CloseNoteDurations[] = {12, 8};
#define playOpenMelody() playMelody(OpenMelody, OpenNoteDurations, 2)
#define playCloseMelody() playMelody(CloseMelody, CloseNoteDurations, 2)
char inChar;
String inString;
void setup() {
pinMode(PIEZO, OUTPUT);
pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, HIGH);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.print(" ENTER PASSWORD");
}
void loop() {
customKey = myKeypad.getKey();
if (customKey) // makes sure a key is actually pressed, equal to (customKey != NO_KEY)
{
tone(8, NOTE_G5, 100);
Data[data_count] = customKey; // store char into data array
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)) // equal to (strcmp(Data, Master) == 0)
{
playOpenMelody();
digitalWrite(RELAY, LOW);
lcd.clear();
lcd.print(" DOOR OPEN");
delay(5000); //time for open the door
lcd.clear();
lcd.print(" DOOR LOCK");
digitalWrite(RELAY, HIGH);
playCloseMelody();
delay(1000);
lcd.clear();
lcd.print(" ENTER PASSWORD");
}
else
{
lcd.clear();
lcd.print(" WRONG PASSWORD");
delay (1000);
lcd.clear();
lcd.print(" ENTER PASSWORD");
playCloseMelody();
}
clearData();
}
}
void clearData()
{
while (data_count != 0)
{ // This can be used for any array size,
Data[data_count--] = 0; //clear array for new data
}
return;
}
void playMelody(int *melody, int *noteDurations, int notesLength)
{
pinMode(PIEZO, OUTPUT);
for (int thisNote = 0; thisNote < notesLength; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(PIEZO, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(PIEZO);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment