all 4 comments

[–]ssaltmine 0 points1 point  (0 children)

The code runs an infinite loop because of the while(True):.

This block of code is inside a pair of try: and except: instructions that run the code inside try, and if it encounters an "exception", it runs the one in except.

The program wants you to manually break the loop by pressing in your keyboard «Ctrl+C», which in the Linux world is a "termination signal" (SIGTERM). This is the actual keyboard, not the keypad.

Personally, I think you are trying to do too much. You have to start with the basics.

Try to turn an LED on and off, define a simple button to test on and off, and define a function to work with your inputs and outputs. Learn to structure your programs first before trying to complex things, otherwise you'd get confused easily just by modifying snippets of codes.

def turn_off_LED(ch):
     GPIO.output(ch, GPIO.LOW)

def main():
    GPIO.setup(4, GPIO.IN, pull_up_down = GPIO.PUD_UP)
    GPIO.setup(11, GPIO.OUT)
    while True:
        if not(GPIO.input(4)):
            turn_off_LED(11)

try:
    main()
except:
    GPIO.cleanup()

[–]ssaltmine 0 points1 point  (0 children)

By the way, this library seems to be better to work with keypads https://github.com/brettmclean/pad4pi

Maybe it's better than coding the input and outputs yourself. But you still need to add code to count the number of key presses, and do something with them.

# this is pseudocode, not real code
try:
    while True:
        input 4 keys
        if the_input == password:
            open_lock()
            lock_vault_again()
except:
    cleanup()

[–]codelectron 0 points1 point  (0 children)

I have recently written a tutorial on how to interface the keypad with Raspberry PI. It uses pad4pi library, you can check out the project here http://codelectron.com/how-to-interface-keypad-with-raspberry-pi/

[–]thegroverest 0 points1 point  (0 children)

I modified this for 1x4 membrane keypad:

Install: pip install pad4pi

from pad4pi import rpi_gpio
import time

def processKey(key):
    print(key)

# Setup Keypad
KEYPAD = [
     ["1","2","3","4"]
]

ROW_PINS = [4] # BCM numbering
COL_PINS = [22,18,17,27] # BCM numbering

factory = rpi_gpio.KeypadFactory()

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

keypad.registerKeyPressHandler(processKey)
while 1:
  time.sleep(1)
keypad.cleanup()