Python Forum
keypad loops/subroutine help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
keypad loops/subroutine help
#1
Hey there, I am stuck on some code I am working on. I am pretty new to coding and this is my first project with Python so i'm sure this chunk of code has much to be desired. Basically I am looking to create a locker system where one can enter a password on a keypad (im using a 4x3 keypad from adafruit along with a raspberry pi 3B+ and a pad4pi library)and actuate a lock solenoid. Additionally I want the user to be able to set their own password of their choosing after pressing a specific button (in this case, *). So far I have been able to manage using the keypad and a default password to actuate the lock but I am running into problems when trying to implement the changing password part. Mainly not being able to change it more than once and the pressed key no longer printing after the first one is pressed(but the key press is still recognized) Any advice/help would be greatly appreciated! Smile

from pad4pi import rpi_gpio
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(20, GPIO.OUT)

KEYPAD = [['1','2','3'],
          ['4','5','6'],
          ['7','8','9'],
          ['*','0','#']]

ROW_PINS = [17, 25, 24, 22]
COL_PINS = [27, 4, 23, 18]

factory = rpi_gpio.KeypadFactory()

keypad = factory.create_keypad(keypad=KEYPAD, row_pins=ROW_PINS, col_pins=COL_PINS)

def defaultPassword():
password='1234'
    return defaultPassword

def changePassword():
    newPassword=input('enter new password')
    return newPassword

def testNewPassword(key):
    for j in range(3):
            GPIO.setup(COL_PINS[j], GPIO.OUT)
            GPIO.output(COL_PINS[j], 1)

    for i in range(4):
            GPIO.setup(ROW_PINS[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
            
    password=changePassword()     
    attempt = ""
        
    try:
            while (True):
                for j in range(3):
                    GPIO.output(COL_PINS[j], 0)

                    for i in range(4):
                        if GPIO.input(ROW_PINS[i]) == 0:
                            time.sleep(0.01)
                            while (GPIO.input(ROW_PINS[i]) == 0):
                                pass
                            attempt += KEYPAD[i][j]
                            if len(attempt) == len(password):
                                if attempt == password: #open lock
                                    print ("Password Correct")
                                    GPIO.setup(21, GPIO.OUT)
                                    GPIO.output(20, GPIO.HIGH)
                                else:   #keep lock closed
                                    print ("Password Incorrect")
                                    GPIO.setup(21, GPIO.IN)
                                    GPIO.output(20, GPIO.LOW)
                                attempt=""
                    GPIO.output(COL_PINS[j], 1)
    except KeyboardInterrupt:
            GPIO.cleanup()

def testDefaultPassword(key):
    for j in range(3):
            GPIO.setup(COL_PINS[j], GPIO.OUT)
            GPIO.output(COL_PINS[j], 1)

    for i in range(4):
            GPIO.setup(ROW_PINS[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
            
    password=defaultPassword()        
    attempt = ""
        
    try:
            while (True):
                for j in range(3):
                    GPIO.output(COL_PINS[j], 0)

                    for i in range(4):
                        if GPIO.input(ROW_PINS[i]) == 0:
                            time.sleep(0.01)
                            while (GPIO.input(ROW_PINS[i]) == 0):
                                pass
                            attempt += KEYPAD[i][j]
                            if len(attempt) == len(password):
                                if attempt == password: #open lock
                                    print ("Password Correct")
                                    GPIO.setup(21, GPIO.OUT)
                                    GPIO.output(20, GPIO.HIGH)
                                else:   #keep lock closed
                                    print ("Password Incorrect")
                                    GPIO.setup(21, GPIO.IN)
                                    GPIO.output(20, GPIO.LOW)
                                attempt=""
                    GPIO.output(COL_PINS[j], 1)
    except KeyboardInterrupt:
            GPIO.cleanup()

def printKey(key):
    print(key)
    if (key=='*'):
        changePassword()
        testNewPassword(key)
    else:
        testDefaultPassword()

#printKey will be called each time a button is pressed
keypad.registerKeyPressHandler(printKey)
Reply
#2
It would be more readable if you use standard (PEP8) indentation of 4 spaces.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding a subroutine to a larger program Led_Zeppelin 2 912 Jan-18-2023, 11:02 PM
Last Post: deanhystad
  Keypad Help FireBlitz8404 1 2,139 Mar-22-2018, 07:31 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020