// Install from the Library Manager: // "Keypad" and "LiquidCrystal I2C" #include #include #include #include #include // The keypad: 4 rows, 4 columns 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'} }; // The socket's 8 pins left to right: the // 4 rows first, then the 4 columns byte rowPins[ROWS] = {9, 8, 7, 6}; byte colPins[COLS] = {5, 4, 3, 2}; const int SERVO_PIN = 10; // The servo's locked and open angles const int LOCKED_ANGLE = 0; const int OPEN_ANGLE = 90; // How long the door stays open (ms) const unsigned long OPEN_TIME = 5000; // This many wrong codes in a row: wait const int MAX_TRIES = 3; const unsigned long BLOCK_TIME = 30000; // While changing the code, give up after // this long without a key (ms) const unsigned long IDLE_TIME = 20000; // Codes are 4-8 digits; 1234 at first const byte MIN_LEN = 4; const byte MAX_LEN = 8; // Forgot the code? Change this number // and upload: the code is 1234 again const byte EEPROM_MARK = 42; // I2C address: 0x27 on most, 0x3F on some LiquidCrystal_I2C lcd(0x27, 16, 2); Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); Servo lock; enum State { LOCKED, UNLOCKED, BLOCKED, NEW_CODE, CONFIRM_CODE }; State state = LOCKED; char code[MAX_LEN + 1] = "1234"; char entry[MAX_LEN + 1] = ""; char newCode[MAX_LEN + 1] = ""; byte entryLen = 0; int wrongTries = 0; unsigned long stateStart = 0; int shownSeconds = -1; // EEPROM: byte 0 a mark, byte 1 the // length, then the digits void loadCode() { if (EEPROM.read(0) != EEPROM_MARK) return; byte len = EEPROM.read(1); if (len < MIN_LEN || len > MAX_LEN) return; for (byte i = 0; i < len; i++) { code[i] = EEPROM.read(2 + i); } code[len] = '\0'; } void saveCode() { byte len = strlen(code); EEPROM.update(0, EEPROM_MARK); EEPROM.update(1, len); for (byte i = 0; i < len; i++) { EEPROM.update(2 + i, code[i]); } } // Both lines of the screen at once void show(const char *top, const char *bottom) { lcd.clear(); lcd.print(top); lcd.setCursor(0, 1); lcd.print(bottom); } // Show a short notice and wait void message(const char *text) { show(text, ""); delay(1500); } // New state; clear what was typed void prompt(State next) { state = next; entryLen = 0; entry[0] = '\0'; if (next == NEW_CODE) { show("New code:", ""); } else if (next == CONFIRM_CODE) { show("Repeat code:", ""); } else { show("Enter code:", ""); } } // Count down at the bottom line's end void showRemaining(unsigned long total) { int left = (total - (millis() - stateStart) + 999) / 1000; if (left == shownSeconds) return; shownSeconds = left; lcd.setCursor(12, 1); if (left < 10) lcd.print(' '); lcd.print(left); lcd.print("s "); } void startTimer(State next) { state = next; stateStart = millis(); shownSeconds = -1; } void openDoor() { wrongTries = 0; lock.write(OPEN_ANGLE); startTimer(UNLOCKED); show("Door open", "Closes in:"); Serial.println("Right code: door opened."); } void lockDoor() { lock.write(LOCKED_ANGLE); prompt(LOCKED); Serial.println("Door locked."); } // Give up changing the code void cancelChange() { prompt(LOCKED); Serial.println("Code change cancelled."); } void wrongCode() { wrongTries++; Serial.print("Wrong code, try "); Serial.println(wrongTries); if (wrongTries >= MAX_TRIES) { startTimer(BLOCKED); show("Too many tries", "Wait:"); Serial.println("Too many tries: keys locked."); return; } show("Wrong code!", "Tries left:"); lcd.setCursor(11, 1); lcd.print(MAX_TRIES - wrongTries); delay(1500); prompt(LOCKED); } // On #, act on what was typed void submit() { if (state == LOCKED) { if (strcmp(entry, code) == 0) { openDoor(); } else { wrongCode(); } } else if (entryLen < MIN_LEN) { message("Min 4 digits!"); prompt(state); } else if (state == NEW_CODE) { strcpy(newCode, entry); prompt(CONFIRM_CODE); } else if (strcmp(entry, newCode) == 0) { strcpy(code, newCode); saveCode(); Serial.println("Code changed and saved."); message("Code changed"); prompt(LOCKED); } else { Serial.println("The codes did not match."); message("No match!"); prompt(NEW_CODE); } } void setup() { Serial.begin(9600); lcd.init(); lcd.backlight(); lock.attach(SERVO_PIN); lock.write(LOCKED_ANGLE); loadCode(); prompt(LOCKED); Serial.println("Lock ready."); } void loop() { char key = keypad.getKey(); if (state == UNLOCKED) { // Locks when time is up or on # if (key == '#' || millis() - stateStart >= OPEN_TIME) { lockDoor(); } else if (key == 'A') { // Lock first, then ask for the code lock.write(LOCKED_ANGLE); startTimer(NEW_CODE); prompt(NEW_CODE); } else { showRemaining(OPEN_TIME); } return; } if (state == BLOCKED) { // Keys are ignored while waiting if (millis() - stateStart >= BLOCK_TIME) { wrongTries = 0; prompt(LOCKED); Serial.println("Wait over."); } else { showRemaining(BLOCK_TIME); } return; } // While changing the code, every key // starts the time over if (state != LOCKED) { if (key) { stateStart = millis(); } else if (millis() - stateStart >= IDLE_TIME) { cancelChange(); return; } } if (!key) return; if (key >= '0' && key <= '9') { // Digits show on screen as * if (entryLen < MAX_LEN) { entry[entryLen++] = key; entry[entryLen] = '\0'; lcd.setCursor(entryLen - 1, 1); lcd.print('*'); } } else if (key == '*') { // Delete the last digit if (entryLen > 0) { entryLen--; entry[entryLen] = '\0'; lcd.setCursor(entryLen, 1); lcd.print(' '); } } else if (key == '#') { submit(); } else if (key == 'D' && state != LOCKED) { cancelChange(); } }