Project - Rotary Combination Lock

This Rotary Digital Combination Lock is as secure as you wish to make it.
Security is maximised by the exact number of correct digits being required.

User combination codes may have any amount of different numbers, so can be easy to remember, or long and complicated... or have both.
More than one user combination code can be set if wished, so different user pass codes might allow access to different features or security levels.

Numbers can range between whatever rmin and rmax limits are set,
ie: the rotary control might count from 0 to 9 to 0 again, or 1010 to 5000.

Simple operation: Power-up or long-hold the button for 2 secs to reset.
The rotary control sequences from rmin to rmax then jumps back to rmin.
Dial each number in the correct order then press the button to register it.
Unlocks after registering the last number of a correctly entered sequence.
Shows which Users code combination was used to unlock, and allows for different User unlock events, ie: user1 switches relay1, user3 relay3 etc.





The simple circuit uses only a rotary encoder and a TM1637 4-digit display with a pair of 10K pullups for its CLK and DIO connections.

You may wish to optionally use an output for a relay or door-release.

If your rotary encoder does not have an integral switch (connected by the thinner rear wires in the diagram) then use a separate push-button.

Basic:
'                                             Rotary Combination Lock
rotaryA = 4                            'Rotary encoder pulse A output pin
rotaryB = 5                            'Rotary encoder pulse B output pin
buttonpin = 0                         'Rotary encoder selector button pin
combination1$ = "1 2 3 4"     'User 1 combination, change to suit
combination2$ = "0 1 9 99"   'User 2 combination, change to suit or make blank "" to disable
combination3$ = ""                'User 3 combination, change to suit or make blank "" to disable
TM1637.SETUP 12, 13         'Enter your appropriate SDA and SCL pins
TMbrightness = 4                  '7=bright, 1=dim, 0=off
rmin = 0                                 'Range minimum, lower values will wrap around to rmax
rmax = 99                              'Range maximum, higher values will wrap around to rmin
d = rmin                                 'Initial number to start from when rotary controller is first moved
'debounce = 20
debounce = 120
lasttime = 0
thistime = 0
start = 0
stop = 0
digits$ = ""
sequence$ = ""
TM1637.PRINT digits$, 4
pin.mode buttonpin, input, pullup
pin.mode rotaryA, input, pullup
pin.mode rotaryB, input, pullup
interrupt rotaryA, changed
interrupt buttonpin, button
wait

changed:
thistime = millis
if thistime - lasttime > debounce then
 if pin(rotaryA) = 0 then
  if pin(rotaryB) = 0 then
   d = d - 1
   if d < rmin then d = rmax
  else
   d = d + 1
   if d > (rmax - 1) then d = rmin
  endif
  TM1637.PRINT str$(d), 4
 wlog str$(d)
  lasttime = thistime
 endif
endif
return

button:
if pin(buttonpin) = 0 then start = millis else stop = millis
if stop > (start + debounce) then
 if stop - start < 2000 then
  'getdigit
  digits$ = digits$ + " " + str$(d)
  wlog digits$
  if combination1$ >"" and trim$(digits$) = combination1$ then
   TM1637.PRINT "   1", 4
   wlog "unlocked User 1"
   d = 0
   digits$ = ""
   sequence$ = ""
   pause 1000
  endif
  if combination2$ >"" and trim$(digits$) = combination2$ then
   TM1637.PRINT "   2", 4
   wlog "unlocked User 2"
   d = 0
   digits$ = ""
   sequence$ = ""
   pause 1000
  endif
  if combination3$ >"" and trim$(digits$) = combination3$ then
   TM1637.PRINT "   3", 4
   wlog "unlocked User 3"
   d = 0
   digits$ = ""
   sequence$ = ""
   pause 1000
  endif
 else
  'reset
  d = 0
  TM1637.PRINT "", 4
  digits$ = ""
  sequence$ = ""
  wlog "reset"
 endif
endif
return

'----------- End ----------



If electronic vaults had no manual backup option then a lot of valuables would be locked away for eternity... so think about your own backup plan.
A simple way to supply external backup power to a locked-away flat battery is to provide eg: a couple of external screw heads as battery terminals.
Electric door releases can typically be used with a standard key operated lock, but you might want to prevent key access while it is powered on ok.
 
Robin Baker,
Jul 4, 2018, 3:03 PM
v.1