I would recommend doing some research on finite state machines, it can really clarify your code in situations like this.
e.g. consider the lock to start in a locked state, say state 0... red on
a specific correct button press takes you to state 1, a bad one takes to back to state 0
subsequently a correct button takes you to state 2, a bad one to state 0
etc
eventually the lock will reach the open state.
I would conjecture that processing it as a stream of button presses is a far cleaner implementation. The logic this is:
- Get the next button press
- Do the last X button presses have no more than Y seconds between presses?
- Do the last X button presses match the secret code?
- If the last two questions are true, then take the action desired for that secret code
You can then do lots of extra things you can't do cleanly with an FSM implemented solution, like have multiple different codes of different lengths