Project showcase
Electronic Safe with Arduino

Electronic Safe with Arduino © GPL3+

Arduino Safe - 6 digit Passcode

  • 28,139 views
  • 30 comments
  • 59 respects

Components and supplies

Apps and online services

About this project

Hello, and welcome to my latest project! In this showcase I will be displaying the safe with an Arduino Uno! This project includes a 6 digit password (123142), that must be entered if you want to unlock the safe.

Buttons

There are 4 buttons to submit characters(1, 2, 3, or 4), an enter button, and a clear screen/lock button. When the safe is unlocked, it will remain unlocked until the button to lock it is pressed. Above are two pictures - the inside, and the outside. The Arduino reads if the button is being pressed or not, and if they are, it displays the corresponding number, and stores that value. When the enter button is pressed, the code that was typed is submitted, and it will unlock if it is correct. If the clear button is clicked, the values will be reset. 

The Servo

The locking mechanism is simple - a servo motor is mounted to the shoebox, and a slit is cut in the shoebox and the door flap. When in the locked position, the motor is turned in between the openings, making it not possible to open.

Above is a photo of the servo in the unlocked position. This allows the shoebox to open and close with ease. When it is turned 90° and the door is shut, it isn't possible to open. When the door is unlocked and you click the clear button, the safe returns to being locked.  Below is a YouTube demonstration, as well as an Instructable page demonstrating how to build it. 

Instructable - http://www.instructables.com/id/Electronic-Safe-With-Arduino/

Code

CodeArduino
/* 
Made by Marco Fusco
May 20, 2016
Contact me at: marcofusco111@gmail.com
*/
   

#include <LiquidCrystal.h>
#include <EEPROM.h>
#include <Servo.h>

int address = 0;
static unsigned long SaveTimer;
static unsigned long SaveDelay = (30 * 1000); 


char CODE[10] = "1234E"; 
char Str[10]; 
char CodeLength = 4;
int Pos = 0;
bool Unlocked;
static unsigned long DisplayTimer; 
static unsigned long DisplayDelay = 200;

LiquidCrystal lcd(12, 11, 9, 8, 7, 6); 


int buttonPin1 = 2;   
int buttonPin2 = 3;
int buttonPin3 = 4;
int buttonPin4 = 5;

int enterbutton = 10; 
int clearlockbutton = 13;

Servo myServo;

void setup() {

  myServo.attach(A1);
  
  int EEPROMCodeOK = true;
  for (Pos = 0; Pos <= (CodeLength); Pos++) {
    Str[Pos] =  EEPROM.read(Pos);
    if (!(strrchr("1123456789", Str[Pos]))) { 
      // not a valid code
      EEPROMCodeOK = false;
    }
  }
  Pos++;
  Str[Pos] =  EEPROM.read(Pos); 
  if (Str[CodeLength + 1] != 'E') EEPROMCodeOK = false; 
  if (EEPROMCodeOK) {
    Str[CodeLength + 2] = '\0';
    strncpy(CODE, Str, CodeLength + 1);
  }
  ClearCode();

  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);
  pinMode(buttonPin4, INPUT_PULLUP);

  pinMode(enterbutton, INPUT_PULLUP);
  pinMode(clearlockbutton, INPUT_PULLUP);

  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("Hello.");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Password:");

  DisplayTimer = millis() + 200;
}


void loop() {
  
  Lock();
  
  Pos = constrain(Pos, 0, CodeLength);

  int buttonState1 = digitalRead(buttonPin1);  
  int buttonState2 = digitalRead(buttonPin2);
  int buttonState3 = digitalRead(buttonPin3);
  int buttonState4 = digitalRead(buttonPin4);

  int clButtonState = digitalRead(clearlockbutton);
  int enterButtonState = digitalRead(enterbutton);

  lcd.setCursor(9, 0);

  if (buttonState1 == LOW) {
    Str[Pos] = '1';
    Pos++;
    Str[Pos] = '\0';
    delay(250); 
    while (digitalRead(buttonPin1) == LOW); 

  }

  else if (buttonState2 == LOW) {
    Str[Pos] = '2';
    Pos++;
    Str[Pos] = '\0';
    delay(250); 
    while (digitalRead(buttonPin2) == LOW);

  }

  else if (buttonState3 == LOW) {
    Str[Pos] = '3';
    Pos++;
    Str[Pos] = '\0';
    delay(250); 
    while (digitalRead(buttonPin3) == LOW);
  }

  else if (buttonState4 == LOW) {
    Str[Pos] = '4';
    Pos++;
    Str[Pos] = '\0';
    delay(250); 
    while (digitalRead(buttonPin4) == LOW); 

  }
  else if (enterButtonState == LOW) {
    Str[Pos] = 'E';
    Pos++;
    Str[Pos] = '\0';
    delay(250);
    while (digitalRead(buttonPin1) == LOW); 
     if (strcmp (Str,CODE) == 0) {
      Unlocked = true;
      lcd.setCursor(0, 0);
      lcd.print(" Access Granted");
      delay(2000);
      lcd.clear();
      lcd.print("    Unlocked");
    } 
    else if (SaveTimer > millis() && (Pos + 1) == CodeLength) { 

      strcpy(CODE, Str);
      for (Pos = 0; Pos <= (CodeLength + 1); Pos++) {
        EEPROM.write(Pos, Str[Pos]);
      }
      lcd.setCursor(0, 0);
      lcd.print("Saving Code:");
      lcd.setCursor(0, 1);
      lcd.print(Str);

      Unlocked = true;
    }

    else { 

      lcd.clear();
      lcd.print(" Access Denied.");
      delay(2000);
      lcd.clear();
      lcd.print("Password:");

    }

    while (Unlocked) {
      Unlock();
      if (digitalRead(clearlockbutton) == LOW) {
        delay(200);
        lcd.clear();
        lcd.print("     Locked");
        delay(2000);
        lcd.clear();
        Unlocked = false;
        SaveTimer = millis() + 30000;
      }
    }
    
    ClearCode();

    
  }

  else if (clButtonState == LOW) {
    delay(500);

    while (clearlockbutton == LOW);
    if ((millis() - SaveTimer) > 4500) {
      
    }
    
    ClearCode();
    
  }

  if ( (long)( millis() - DisplayTimer ) >= 0) {
    DisplayTimer += DisplayDelay;
    lcd.setCursor(9, 0); 
    lcd.print(Str);
    lcd.print("     ");

  }
}

void ClearCode() {
  
  Pos = 0;
  Str[Pos] = '\0';
  lcd.setCursor(0, 0);
  lcd.print("Password:");
  lcd.setCursor(0, 1);
  lcd.print("          ");
  
}

void Unlock() {

  myServo.write(150);
  
}

void Lock() {

  myServo.write(50);

}
Arduino Create Link

Schematics

Diagram
Screen%20shot%202016 05 20%20at%204.03.28%20pm

Comments

Similar projects you might like

Electronic Piano Keyboard With Preset Songs

Project tutorial by Lindsay Fox

  • 77,975 views
  • 60 comments
  • 150 respects

Safe City: A Device to Keep You Safe

Project tutorial by Patel Darshil

  • 7,683 views
  • 0 comments
  • 24 respects

Cracking an electronic safe using brute force

Project tutorial by Bob Anderson

  • 5,682 views
  • 1 comment
  • 20 respects

Simple Programmable Robotic Arm

Project showcase by Ryan Chan

  • 50,057 views
  • 65 comments
  • 146 respects

Suicide Prevention Gun Safe Locking System

Project showcase by 6 developers

  • 8,725 views
  • 10 comments
  • 17 respects

Electronic Music

Project showcase by Glauber Santiago

  • 1,027 views
  • 1 comment
  • 0 respects
Add projectSign up / Login