#include <Keypad.h>
#include <textBuff.h>
#include <timeObj.h>
#include <resizeBuff.h>
#define PASSCODE "21137020"
#define UNLOCK_LED_PIN 10
#define ROWS 4
#define COLS 4
#define NUM_CHARS 80
#define ERROR_MS 500
#define PASS_MS 5000
#define BEEPER_PIN 11
#define CLICK_FREQ 600
#define CLICK_FREQ_MS 25
#define UNLOCK_FREQ 800
#define UNLOCK_FREQ_MS 100
#define ERROR_FREQ 100
#define ERROR_FREQ_MS 500
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 5, 4, 3, 2 };
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
textBuff ourMsg(NUM_CHARS);
bool locked;
bool badStr;
timeObj badStrTimer(ERROR_MS,false);
timeObj lockTimer(PASS_MS,false);
void setup() {
Serial.begin(9600);
locked = false;
badStr = false;
pinMode(UNLOCK_LED_PIN,OUTPUT);
Serial.println("Enter password ");
Serial.println("press # to unlock");
}
void checkStr(void) {
char* passStr;
passStr = ourMsg.readStr();
ourMsg.clear();
if (!strcmp(PASSCODE,passStr)) {
unlock();
} else {
showError();
}
}
void lock(void) {
locked = true;
lockTimer.reset();
Serial.println("Relocking.");
}
void unlock(void) {
locked = false;
lockTimer.start();
Serial.println("Success!");
tone(BEEPER_PIN, UNLOCK_FREQ, UNLOCK_FREQ_MS);
}
void showError(void) {
badStr = true;
badStrTimer.start();
Serial.println("!!!!!!!entered password is incorrect!!!!");
tone(BEEPER_PIN, ERROR_FREQ, ERROR_FREQ_MS);
}
void clearError(void) {
badStr = false;
badStrTimer.reset();
}
void loop() {
char key;
key = keypad.getKey();
if (key != NO_KEY) {
tone(BEEPER_PIN, CLICK_FREQ, CLICK_FREQ_MS);
if (key=='#') {
Serial.println();
Serial.println("#");
checkStr();
} else {
Serial.print(key);
ourMsg.addChar(key);
}
}
if (!locked && lockTimer.ding()) {
lock();
}
if (badStr && badStrTimer.ding()) {
clearError();
}
digitalWrite(UNLOCK_LED_PIN,!locked);
}