0

Is there any example on how to use the pad4pi library from Github to help write a simple door lock python program? (Pressing 1 to unlock solenoid lock, turned on; press 2 to lock, turned off). As shown in the code, this program is kinda tedious that you have to write these codes in order to read inputs by the RPi (turn off row by row in order to check inputs). Does pad4pi reduces that tediousness? If so, how to implement it into the program?

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)     #Disable warnings.

GPIO.setmode(GPIO.BCM)      #Setup BCM GPIO numbering.
GPIO.setup(12, GPIO.OUT)    #Set GPIO12 as output.

COL = [16,20,21]    #BCM Numbering for Columns.

GPIO.setup(6,GPIO.OUT)  #Set Row 1 as output.
GPIO.output(6,1)        #Set Row 1 high.

for j in range(2):
    GPIO.setup(COL[j], GPIO.IN, pull_up_down = GPIO.PUD_UP) #Defining input GPIOs with pull-up resistor for Col 1 and 2.


#--------Main program--------
try:
    while(True):            #Never-ending loop unless ctrl+c is pressed (which goes to KeyboardInterrupt).
        GPIO.output(6,0)   #Set ROW 1 low first, to check inputs (i.e. which button is pressed).

        if GPIO.input(COL[0]) == 0:     #When '1' is pressed,
            GPIO.output(12, True)       #unlock. (i.e. GPIO12 goes high)

            while GPIO.input(COL[1]) == 1:  #If '2' is not pressed yet,
                GPIO.output(12, True)       #stays unlocked.

            if GPIO.input(COL[1]) == 0:     #If '2' is pressed,
                GPIO.output(12, False)      #lock.

        else:
            GPIO.output(12, False)      #If '1' is not pressed, then it stays locked.


        GPIO.output(6,1)   #Set all Row 1 back to high.


except KeyboardInterrupt:   #When CTRL+C is pressed,
    GPIO.cleanup()          #Resetting GPIO to defaults, then ends the program.
  • Hi @Shawn Khoo, Well the program uses the generic standard scanning way to check which key of a 4x4, 8x8, etc keypad is pressed. But if you are using only 4 keys say, you can use a 4 key pad, and connect each key switch to one Rpi GPIO pin. For 16 keys, if you want, you can use MCP23017, connect one key switch to 1 of 16 MCP23017 IO Port Extender GPIO pins. So there is no key scanning, only interrupt GPIO pins, and the program is very simple, not tedious at all! :) Reference: raspberrypi.stackexchange.com/questions/98440/… – tlfong01 Sep 14 at 1:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.