So my below code is me trying to grab individual number inputs from a membrane keypad and pass them into a variable. Obviously I need to keep adding the new character to the variable ‘finalPW’ until it is 4 digits long and then of course I need to return it to be used in a function that compares it to a list of other strings.
My issue is that the variable is either reset each time, so I only ever get one digit, or it simply states that the ‘finalPW’ variable is referenced before its assigned. Been working on it like 3 weeks now to no avail!
Help me Oracles!
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) MATRIX = [['D', '#', '0', '*'], ['C', '9', '8', '7'], ['B', '6', '5', '4'], ['A', '3', '2', '1']] COL = [21, 20, 16, 26] ROW = [19, 13, 6, 5] def passwordinit(): finalPW = "" return finalPW for j in range(4): GPIO.setup(COL[j], GPIO.OUT) GPIO.output(COL[j], 1) for i in range(4): GPIO.setup(ROW[i],GPIO.IN,pull_up_down=GPIO.PUD_UP) def keypadcharacter(): finalPW = "" if len(finalPW) < 4: try: while (True): for j in range(4): GPIO.output(COL[j] ,0) for i in range (4): if GPIO.input(ROW[i]) ==0: pwS = MATRIX[i][j] time.sleep(0.5) finalPW += pwS print finalPW while(GPIO.input(ROW[i]) ==0): pass GPIO.output(COL[j],1) except KeyboardInterrupt: GPIO.cleanup() return finalPW else: print finalpw passwordinit() keypadcharacter()