# Creating a combination lock using logic gates?

I am trying to figure out how to create a combination lock using logic gates, so that a password must be input (let's say pressing numbered buttons) in the correct order.

I understand logic gates completely, and have some of a grasp on flip-flops and latches, but I'm definitely getting stuck on how to use them in the right combination.

So as an example, if there are 3 buttons (call them 1, 2, & 3), I would like to be able to set a password such as 1 2 3 1 (hardcoded is fine), where they have to be input in that exact order, otherwise you have to start at the beginning again (1 2 3 3 3 3 1 will not work).

Any tips, links, hints on this?

-
Tangential comment: You can most likely implement the whole thing in a CPLD quicker, easier and cheaper than using discrete logic components. Added benefit is that the attacker cannot see how the components are wired together. The question is still very relevant though, as you have to construct the whole thing out of logic gates one way or the other. –  drxzcl Feb 16 '11 at 9:24

I would probably use two bits to encode the three switches, and the general approach would be to have a string of pairs (one for each bit) of D or JK flip-flops to accept the sequence of input digits, essentially a shift register for pairs of bits. Then there would have to be de-bounce circuitry for the switches, and some way to generate a clock signal from the key presses (probably on release) to shift the digits through the register. Finally, XOR gates between the stages of the shift register and the digits of the key (hardwired to 1231 or use switches, etc) would produce signals to indicate when each digit is a correct match (logic Low on match), and then a multi-input NOR of the XOR outputs would give a final output signal that would indicate when the entire sequence in the shift register matches the key.

-
So you're saying make 1,2,3 into 00, 01, 10, then cycle through the password press by press, testing each press against the stored code in the shift register? –  JustcallmeDrago Feb 16 '11 at 3:38
not exactly- first press would enter a pair of bits to the 1st stage of the shift register. 2nd press would shift the 1st code to the 2nd stage, and enter the new code in the 1st stage, and so on, so that at any point in time, if you have say 5 stages of shift register, you have the last 5 button presses represented. This 'latest-5' would be checked against the key value all the time. I.e., the key is not stored in the shift register, the key is just hardwired inputs to the XORs, or some hidden switch inputs to them; the shift reg stores the user input. –  JustJeff Feb 16 '11 at 11:53
Makes sense, thanks a lot! The shift register is the component I needed. –  JustcallmeDrago Feb 16 '11 at 18:38