#include <Password.h>
#include <Keypad.h>
#include <Servo.h>
int pinSpeaker = 10;
Servo myservo;
Password password = Password("1308");
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] = {9,8,7,6};
byte colPins[COLS] = {5,4,3,2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins,
ROWS, COLS);
int counter = 0;
void setup() {
Serial.begin(9600);
delay(200);
pinMode(pinSpeaker, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
myservo.attach(13);
keypad.addEventListener(keypadEvent);
// put your setup code here, to run once:
}
void loop() {
keypad.getKey();
myservo.write(0);
// put your main code here, to run repeatedly:
}
void keypadEvent(KeypadEvent eKey) {
switch (keypad.getState()) {
case PRESSED:
Serial.print("Pressed: ");
Serial.println(eKey);
switch (eKey) {
case '*': checkPassword(); break;
case '#': password.reset(); break;
default: password.append(eKey);
}
}
}
void checkPassword() {
if (password.evaluate() ){
Serial.println("Success");
myservo.write(90);
digitalWrite(11, HIGH);
digitalWrite(pinSpeaker, LOW);
playTone(0,0);
delay(500);
digitalWrite(11, LOW);
} else {
Serial.println("Wrong");
myservo.write(0);
digitalWrite(12, HIGH);
digitalWrite(pinSpeaker, HIGH);
counter += 1;
if (counter == 3) {
playTone(300, 160);
counter = 0;
delay(500);
}
digitalWrite(12, LOW);
}
}
void playTone(long duration, int freq) {
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration) {
digitalWrite(pinSpeaker, HIGH);
delayMicroseconds(period / 2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}