Now that both the keypad and the display work independently of each other code must be written to make the two of them work together
The Display code can be used by making the CodePad code in the same folder, deleting the while true loop at the end, and importing display along with sys, RPi, and time
Declare the pin numbers from the keypad that the columns and rows are associated with to make their usage easier
Additionally, variables for whether a key has been pressed, what you want the code to be, and an empty string input variable
Now turn off set warning and set the pin mode to BCM
Then setup all the lines of the keypad as simple outputs and the columns as inputs using the internal pull-down resistors
Lastly setup any pins need for what you want the program to do when the code is entered correctly, in this case turn on a LED associated with GPIO 8
Create a function that uses the keypadPressed variable to determine if a key is held down
Detect the rising edges on the column lines of the keypad. This way, we can detect if the user presses a button when a pulse is sent.
Sets all lines to a specific state. This is a helper for detecting when the user releases a button
The next function that needs to be made is one to check for special button cases, those being a reset input button (c) and the enter input button (a)
This is done by raising the requisite lines and checking the respective columns
As part of this the passcode is also checked for whether or not it is correct.
Now, to detect button presses a pulse is sent down each line similar to how the special checker worked. It then appends the detected value to the input variable
The next step is to create a variable for what the program should do upon a correct passcode entry
The main function of the program uses an infinite loop to keep the program going
First check if the button that was previously pressed has been released yet then read the input after either of these output the current input to the Display function
Next reset the input variable if the input is longer than the maximum number of digits on the display
Lastly make an exit condition from a KeyboardInterrupt
Congrats, you should now have a functional keypad lock! Just enter in the numbers and hit A to verify. There is a lot you can do with this from here, like maybe making a solenoid or servo unlock a box or door when you get the code right. Have fun!