[作品提交] 不完善的密码锁只好实现矩阵键盘

yinxx   2024-1-18 16:06 楼主

 作品名称:不完善的密码锁只好实现矩阵键盘
作者:yinxx

# Include the library files
import RPi.GPIO as GPIO
from time import sleep
# Enter column pins
C1 = 5
C2 = 6
C3 = 13
C4 = 19
# Enter row pins
R1 = 12
R2 = 16
R3 = 20
R4 = 21

keypadPressed = -1
# Enter your PIN
secretCode = "1111"
input = ""
# Setup GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

# Set column pins as output pins
GPIO.setup(C1, GPIO.OUT)
GPIO.setup(C2, GPIO.OUT)
GPIO.setup(C3, GPIO.OUT)
GPIO.setup(C4, GPIO.OUT)
# Set row pins as input pins
GPIO.setup(R1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(R2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(R3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(R4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# This callback registers the key that was pressed
# if no other key is currently pressed
def keypadCallback(channel):
    global keypadPressed
    if keypadPressed == -1:
        keypadPressed = channel
# Detect the rising edges
GPIO.add_event_detect(R1, GPIO.RISING, callback=keypadCallback)
GPIO.add_event_detect(R2, GPIO.RISING, callback=keypadCallback)
GPIO.add_event_detect(R3, GPIO.RISING, callback=keypadCallback)
GPIO.add_event_detect(R4, GPIO.RISING, callback=keypadCallback)
# Sets all rows to a specific state. 
def setAllRows(state):
    GPIO.output(C1, state)
    GPIO.output(C2, state)
    GPIO.output(C3, state)
    GPIO.output(C4, state)
# Check or clear PIN
def commands():
    global input
    pressed = False
    GPIO.output(C1, GPIO.HIGH)
    
    # Clear PIN 
    if (GPIO.input(R1) == 1):
        print("Input reset!");
        sleep(1)
        pressed = True
    GPIO.output(C1, GPIO.HIGH)
    # Check PIN
    if (not pressed and GPIO.input(R2) == 1):
        if input == secretCode:
            print("Code correct!")           
        else:
            print("Incorrect code!")         
        pressed = True
    GPIO.output(C1, GPIO.LOW)
    if pressed:
        input = ""
    return pressed
# reads the columns and appends the value, that corresponds
# to the button, to a variable
def read(column, characters):
    global input
    GPIO.output(column, GPIO.HIGH)
    if(GPIO.input(R1) == 1):
        input = input + characters[0]
        print(input)
    if(GPIO.input(R2) == 1):
        input = input + characters[1]
        print(input)
    if(GPIO.input(R3) == 1):
        input = input + characters[2]
        print(input)
    if(GPIO.input(R4) == 1):
        input = input + characters[3]
        print(input)
    GPIO.output(column, GPIO.LOW)
try:
    while True:       
        input = input("Enter your PIN: ")
        
        # If a button was previously pressed,
        # check, whether the user has released it yet
        if keypadPressed != -1:
            setAllRows(GPIO.HIGH)
            if GPIO.input(keypadPressed) == 0:
                keypadPressed = -1
            else:
                sleep(0.1)
        # Otherwise, just read the input
        else:
            if not commands():
                read(C1, ["D","C","B","A"])
                read(C2, ["#","9","6","3"])
                read(C3, ["0","8","5","2"])
                read(C4, ["*","7","4","1"])
                sleep(0.1)
            else:
                sleep(0.1)
except KeyboardInterrupt:
    print("Stopped!")

 

 

 

首先我的想法是充分发挥手中的可以直接上手,最简单的方式使用的,最小方案吧。于是我决定还是弄个矩阵键盘的python程序控制看看。

image.png  

按照这个接线方式进行连线,我不上图了,因为有参考视频。我最后的实现就是这个视频的样子。

 

How to make a door lock security system with a Raspberry Pi board.mp4 (14.05 MB)
(下载次数: 3, 2024-1-18 16:05 上传)


How_to_make_a_door_lock_security_system_with_a_Raspberry_Pi_board

 

回复评论 (1)

矩阵键盘也是比较繁琐的,尤其是硬件部分。

点赞  2024-1-18 17:10
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复