#include <Keypad.h>
#include <Servo.h>
char m[4][4]={{'1','2','3','A'},{'4','5','6','B'},{'7','8','9','C'},{'*','0','#','D'}},b[5],c,q[4]={'1','2','3','4'};uint8_t l[4]={7,6,5,4},r[4]={11,10,9,8},x=0,i,o,e;Keypad k=Keypad(makeKeymap(m),r,l,4,4);Servo s;
void setup(){s.attach(3);s.write(0);}
void loop(){c=k.getKey();if(c=='#'){if(o){for(i=0;i<4;i++){q[i]=b[i];b[i]=-1;}}x=0;}else if(c=='*'){e=1;for(i=0;i<4;i++){e&=q[i]==b[i];}if(e){s.write(o=!o?180:0);}for(i=0;i<5;b[++i]=-1)x=0;}else if(c!=0){b[x]=c;x=++x%4;}}
/*
#include <Keypad.h>
#include <Servo.h>
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 7, 6, 5, 4 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 11, 10, 9, 8 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Servo myservo;
char str[4] = {'1', '2', '3', '4'};
char buffer[5];
int index = 0;
bool open = false;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
myservo.attach(3);
myservo.write(0);
}
void loop() {
// put your main code here, to run repeatedly:
char key = keypad.getKey();
// clear output on #
if (key == '#'){
// reset code when open
if (open){
for (int i=0;i<4;i++) {
str[i]=buffer[i];
}
}
// clear buffer
for (int i=0;i<5;i++){
buffer[i] = -1;
}
index = 0;
// submit on *
} else if (key == '*') {
// compare buffer with preset string
bool check = true;
for (int i=0;i<4;i++){
check = check && (str[i]==buffer[i]);
}
// if they match, servotime
if (check){
open = !open;
myservo.write(open?180:0);
}
for (int i=0;i<5;i++){
buffer[i] = -1;
}
index = 0;
// add input on any other key (if one was pressed)
} else if (key != 0){
buffer[index] = key;
index++;
index = index % 5;
}
if (key != 0){
Serial.println(key);
}
}
*/