Created
October 17, 2019 10:35
-
-
Save suadanwar/3a1f4fc56b08af3d3d7746ed84bc9d4e to your computer and use it in GitHub Desktop.
This sample code is for Safe Box's Tutorial.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <LiquidCrystal_I2C.h> | |
LiquidCrystal_I2C lcd(0x27, 20, 2); | |
#include <Keypad.h> | |
#include <Servo.h> | |
Servo myservo; | |
#define Password_Lenght 7 // Give enough room for six chars + NULL char | |
#define PIEZO 8 | |
#define NOTE_G4 392 | |
#define NOTE_C5 523 | |
#define NOTE_G5 784 | |
#define NOTE_C6 1047 | |
int pos = 0; // variable to store the servo position | |
char Data[Password_Lenght]; // 6 is the number of chars it can hold + the null char = 7 | |
char Master[Password_Lenght] = "123456"; | |
byte data_count = 0, master_count = 0; | |
bool Pass_is_good; | |
char customKey; | |
bool door = true; | |
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] = {10, 9, 7, 6}; | |
byte colPins[COLS] = {5, 4, 3, 2}; | |
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() { | |
// put your setup code here, to run once: | |
myservo.attach(11); | |
pinMode(PIEZO, OUTPUT); | |
ServoClose(); | |
lcd.init(); | |
lcd.backlight(); | |
lcd.print(" Suad Anwar "); | |
lcd.setCursor(0, 1); | |
lcd.print(" Safe Box "); | |
delay(3000); | |
lcd.clear(); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
if (door == 0) | |
{ | |
customKey = myKeypad.getKey(); | |
if (customKey == '#') | |
{ | |
lcd.clear(); | |
ServoClose(); | |
lcd.print(" Closing..."); | |
playCloseMelody(); | |
delay(2000); | |
door = 1; | |
} | |
} | |
else Open(); | |
} | |
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 ServoOpen() | |
{ | |
for (pos = 90; pos >= 0; pos -= 5) { | |
myservo.write(pos); // tell servo to go to position in variable 'pos' | |
delay(15); // waits 15ms for the servo to reach the position | |
} | |
} | |
void ServoClose() | |
{ | |
for (pos = 0; pos <= 90; pos += 5) { | |
myservo.write(pos); // tell servo to go to position in variable 'pos' | |
delay(15); // waits 15ms for the servo to reach the position | |
} | |
} | |
void Open() | |
{ | |
lcd.setCursor(0, 0); | |
lcd.print(" Enter Password "); | |
customKey = myKeypad.getKey(); | |
if (customKey) // makes sure a key is actually pressed, equal to (customKey != NO_KEY) | |
{ | |
Data[data_count] = customKey; // store char into data array | |
lcd.setCursor(data_count, 1); // move cursor to show each new char | |
lcd.print(Data[data_count]); // print char at said cursor | |
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) | |
{ | |
lcd.clear(); | |
ServoOpen(); | |
lcd.print(" Access Granted"); | |
playOpenMelody(); | |
door = 0; | |
} | |
else | |
{ | |
lcd.clear(); | |
lcd.print(" Access Denied "); | |
playCloseMelody(); | |
delay(1000); | |
door = 1; | |
} | |
clearData(); | |
} | |
} | |
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