LoganSpace42
Published

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.

BeginnerFull instructions provided21,285
Arduino Lock Box

Things used in this project

Hardware components

Jumper wires (generic)
Jumper wires (generic)
×25
Resistor 10k ohm
Resistor 10k ohm
×4
Resistor 220 ohm
Resistor 220 ohm
×1
Tactile Switch, Top Actuated
Tactile Switch, Top Actuated
×4
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Liquid Crystal Display
(Generic one that comes with Arduino Starter Kit)
×1
Single Turn Potentiometer- 10k ohms
Single Turn Potentiometer- 10k ohms
(Whatever the Generic Potentiometer in the Arduino Kit is)
×1
Arduino UNO
Arduino UNO
×1
Breadboard (generic)
Breadboard (generic)
×1

Story

Read more

Schematics

Wiring Diagram

Flow Chart

This just shows how the code is thinking and which buttons result in which actions.

Code

Code for Arduino

Arduino
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; 
}
    

Credits

LoganSpace42

LoganSpace42

0 projects • 2 followers

Comments