Project tutorial
Arduino Lock Box

Arduino Lock Box

A quick build for an Arduino Uno that uses a countdown timer to open a servo motor gate in order to use it as a lock.

  • 2,242 views
  • 4 comments
  • 12 respects

Components and supplies

About this project

NOTE:

If you are having trouble with making this project work properly please view this forum link: https://forum.arduino.cc/index.php?topic=621641.0

Apologies to those who have had issues with this. I will update the project as soon as I have time to do so!

So, Why Build This?

This invention is deigned to be a “lock box” with a timer. Once the timer has counted down to zero it sends a signal to rotate the servo motor 90 degrees to unlock the hatch on the box it is installed in. There are four push buttons, if you hold the first to the left, it will bring you into settings. Here you can use the second button from the left to toggle between hours, minutes, and seconds. Using the third and fourth buttons from the left as -/+ buttons respectively, you can add or subtract time from the timer. Once you have selected the amount of time you would like to set the lock for, you can click the button to the far left to bring you back to the start menu. From here, you can press the second button from the left to start your timer lock. Once you start the timer, the servo rotates 90 degrees to effectively “lock” whatever it is installed into. When the timer expires, the servo rotates back 90 degrees the other way and “unlocks” the box. The Potentiometer on the far left can also be used to adjust the brightness of the LCD screen.

Preview of the Finished Build:

Shows Functioning Build

Code

Code for ArduinoArduino
You just upload this to the arduino from the application.
        #include <Time.h> 
#include <TimeLib.h> 
 
#include <LiquidCrystal.h> 
 
#include <EEPROM.h> 
#include <Servo.h> 
 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 
 
Servo myServo; 
 
const int BackButtonPin = 6; 
const int StartButtonPin = 7; 
const int MinusButtonPin = 8; 
const int PlusButtonPin = 9; 
 
int settingHours = 0;    
int settingMinutes = 0;   
int settingSeconds = 0;   
time_t settingTime = 0; 
 
int activeHours = 0; 
int activeMinutes = 0; 
int activeSeconds = 0; 
time_t activeTime = 0;  
 
 
time_t startTime = 0; 
time_t elapsedTime = 0; 
 
int BackButtonState = LOW; 
long BackButtonLongPressTime = 0; 
int StartButtonState = LOW; 
int PlusButtonState = LOW; 
int MinusButtonState = LOW; 
int BackButtonPrevState = LOW; 
int StartButtonPrevState = LOW; 
int PlusButtonPrevState = LOW; 
int MinusButtonPrevState = LOW; 
bool BackButtonPress = false; 
bool BackButtonLongPress = false; 
bool StartButtonPress = false; 
bool PlusButtonPress = false; 
bool MinusButtonPress = false; 
 
bool locked = false; 
 
const int READY = 0; 
const int SETTINGS = 1; 
const int LOCKED = 2; 
const int UNLOCKED = 3; 
 
int activeState = READY;     
  
 
int timeData = 0;   
 
void setup() { 
 
  lcd.begin(16, 2); 
  pinMode(BackButtonPin, INPUT); 
  pinMode(StartButtonPin, INPUT); 
  pinMode(PlusButtonPin, INPUT); 
  pinMode(MinusButtonPin, INPUT); 
  myServo.attach(10); 
  Serial.begin(9600); 
  myServo.write(90); 
  
} 
 
void loop() { 
   
  StartButtonPress = false; 
  PlusButtonPress = false; 
  MinusButtonPress = false; 
 
  BackButtonPress = false; 
  BackButtonLongPress = false; 
  BackButtonState = digitalRead(BackButtonPin); 
  if(BackButtonState != BackButtonPrevState) 
  { 
    BackButtonPress = BackButtonState == HIGH; 
    BackButtonPrevState = BackButtonState;  
 
  } 
  else 
  { 
    if(BackButtonState == HIGH) 
    { 
      BackButtonLongPressTime++; 
      if(BackButtonLongPressTime == 100) 
      { 
        BackButtonPress = false; 
        BackButtonLongPress = true; 
        BackButtonLongPressTime = 0; 
      } 
    } 
    else 
    { 
      BackButtonLongPressTime = 0; 
      BackButtonPress = false; 
      BackButtonLongPress = false; 
    } 
  } 
 
  StartButtonPress = false; 
  StartButtonState = digitalRead(StartButtonPin); 
  if(StartButtonState != StartButtonPrevState) 
  { 
    StartButtonPress = StartButtonState == HIGH; 
    StartButtonPrevState = StartButtonState; 
  }  
 
 
  MinusButtonPress = false; 
  MinusButtonState = digitalRead(MinusButtonPin); 
  if(MinusButtonState != MinusButtonPrevState) 
  { 
    MinusButtonPress = MinusButtonState == HIGH; 
    MinusButtonPrevState = MinusButtonState; 
  } 
 
  PlusButtonPress = false; 
  PlusButtonState = digitalRead(PlusButtonPin); 
  if(PlusButtonState != PlusButtonPrevState) 
  { 
    PlusButtonPress = PlusButtonState == HIGH; 
    PlusButtonPrevState = PlusButtonState; 
  } 
 
  switch(activeState) 
  { 
    case READY: 
    myServo.write(90); 
      if(BackButtonPress) 
      { 
        Reset(); 
      } 
      if(BackButtonLongPress) 
      { 
        activeState = SETTINGS;  
 
      } 
      if(StartButtonPress) 
      { 
        activeState = activeState == READY ? LOCKED : READY; 
        if(activeState == LOCKED) 
        { 
          startTime = now(); 
        } 
      } 
      break; 
 
    case SETTINGS: 
    myServo.write(90); 
      if(BackButtonPress) 
      { 
        settingTime = settingSeconds + (60 * settingMinutes) + (3600 * settingHours); 
        activeHours = settingHours; 
        activeMinutes = settingMinutes; 
        activeSeconds = settingSeconds; 
        timeData = 0; 
        activeState = READY; 
      } 
      if(StartButtonPress) 
      { 
        timeData++; 
        if(timeData == 3) 
        { 
          timeData = 0; 
 
        } 
      } 
      if(MinusButtonPress) 
      { 
        switch(timeData) 
        { 
          case 0:  
            settingHours--; 
            if(settingHours == -1) 
            { 
              settingHours = 99; 
            } 
            break; 
          case 1:  
            settingMinutes--; 
            if(settingMinutes == -1) 
            { 
              settingMinutes = 59; 
            } 
            break; 
          case 2:  
            settingSeconds--; 
            if(settingSeconds == -1) 
            { 
              settingSeconds = 59; 
            } 
            break; 
        }  
 
      } 
      if(PlusButtonPress) 
      { 
        switch(timeData) 
        { 
          case 0:  
            settingHours++; 
            if(settingHours == 100) 
            { 
              settingHours = 0; 
            } 
            break; 
          case 1:  
            settingMinutes++; 
            if(settingMinutes == 60) 
            { 
              settingMinutes = 0; 
            } 
            break; 
          case 2:  
            settingSeconds++; 
            if(settingSeconds == 60) 
            { 
              settingSeconds = 0; 
            } 
            break; 
        } 
      } 
 
      break; 
     
    case LOCKED: 
    myServo.write(0); 
      if(StartButtonPress) 
      { 
        activeState = READY; 
      } 
      if(BackButtonPress) 
      { 
        Reset(); 
        activeState = READY; 
      } 
      break; 
 
    case UNLOCKED: 
    myServo.write(90); 
      if(BackButtonPress || StartButtonPress || MinusButtonPress || PlusButtonPress) 
      { 
        activeState = READY; 
      } 
      break; 
  } 
 
  switch(activeState) 
  { 
    case READY: 
    case SETTINGS:  
 
      break; 
    case LOCKED: 
      activeTime = settingTime - (now() - startTime); 
      if(activeTime <= 0) 
      { 
        activeState = UNLOCKED; 
      } 
      break; 
  } 
 
  lcd.setCursor(0, 0); 
  switch(activeState) 
  { 
    case READY: 
      lcd.print("Ready: Start    "); 
      lcd.setCursor(0, 1); 
      lcd.print(activeHours); 
      lcd.print("  "); 
      lcd.print(activeMinutes); 
      lcd.print("  "); 
      lcd.print(activeSeconds); 
      lcd.print("     "); 
      break; 
    case SETTINGS: 
      lcd.print("Set Timer:  "); 
      switch(timeData) 
      { 
        case 0: 
 
          lcd.print("HRS "); 
          break; 
        case 1: 
          lcd.print("MINS"); 
          break; 
        case 2: 
          lcd.print("SECS"); 
          break; 
      } 
      lcd.setCursor(0, 1); 
      lcd.print(settingHours); 
      lcd.print("  "); 
      lcd.print(settingMinutes); 
      lcd.print("  "); 
      lcd.print(settingSeconds); 
      lcd.print("     "); 
      break; 
    case LOCKED: 
      lcd.print("Waiting...       "); 
      lcd.setCursor(0, 1); 
      if(hour(activeTime) < 10) lcd.print("0"); 
      lcd.print(hour(activeTime)); 
      lcd.print(": "); 
      if(minute(activeTime) < 10) lcd.print("0"); 
      lcd.print(minute(activeTime)); 
      lcd.print(": "); 
      if(second(activeTime) < 10) lcd.print("0"); 
      lcd.print(second(activeTime)); 
 Logan Ritten  
 
      break; 
    case UNLOCKED: 
      lcd.print("   UNLOCKED!   "); 
      lcd.setCursor(0, 1); 
      lcd.print("              "); 
      break; 
  } 
  delay(10); 
} 
 
void Reset() 
{ 
  activeState = READY; 
  activeHours = settingHours; 
  activeMinutes = settingMinutes; 
  activeSeconds = settingSeconds; 
}
    

Schematics

Wiring Diagram
Flow Chart
This just shows how the code is thinking and which buttons result in which actions.

Comments

Author

45f99c2b 240f 4553 aa79 969293d085d9
LoganSpace42
  • 1 project
  • 0 followers

Additional contributors

Published on

June 13, 2019

Members who respect this project

DefaultPhotoDefault150183 10203542998645007 6694829654642781600 n

and 12 others

See similar projects
you might like

Similar projects you might like

[Airbnb] Sigfox Lock Box

Project tutorial by Antoine de Chassey

  • 8,056 views
  • 0 comments
  • 12 respects

Push Button Combination Lock Using Arduino

Project tutorial by Asim Zulfiqar and High Voltages

  • 5,107 views
  • 9 comments
  • 21 respects

Arduino Controlled Lock Box with Solenoid and RFID

Project tutorial by RobotGeek Projects Team

  • 7,046 views
  • 3 comments
  • 20 respects

Rickroll Box

Project showcase by slagestee

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