Skip to content

Instantly share code, notes, and snippets.

@slav123 slav123/keylock.ino
Created Mar 6, 2019

Embed
What would you like to do?
Arduino double relay keypad lock
#include <Keypad.h>
#define PasswordLength 5
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1','2','3', 'A'},
{'4','5','6', 'B'},
{'7','8','9', 'C'},
{'*','0','#', 'D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; //connect to the column pinouts of the keypad
char Data[PasswordLength];
char Master[PasswordLength] = "1234";
char Slave[PasswordLength] = "1235";
byte data_count = 0;
int piezoPin = 13;
int Relay1 = 10;
int Relay2 = 11;
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
//Serial.begin(9600);
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
}
void loop(){
char key = keypad.getKey();
if (key){
// Serial.println(key);
Data[data_count] = key;
data_count++;
if (key == '*') {
clearData();
}
}
if (data_count == PasswordLength-1) {
if (!strcmp(Data,Master)) {
// Serial.println("goood");
tone(piezoPin, 2000, 500);
digitalWrite(Relay1, HIGH);
delay(3000);
digitalWrite(Relay1, LOW);
tone(piezoPin, 2000, 500);
} else if (!strcmp(Data,Slave)) {
tone(piezoPin, 2000, 250);
digitalWrite(Relay2, HIGH);
delay(30000);
digitalWrite(Relay2, LOW);
tone(piezoPin, 2000, 250);
}
else {
//Serial.println("fail");
tone(piezoPin, 1000, 500);
}
//Serial.println(Data);
clearData();
}
}
void clearData(){
while(data_count !=0){
Data[data_count--] = 0;
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.