Python Forum

Full Version: Security Program Help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Not sure how to properly differentiate code between normal talk on here. New to the site, please bear with me. I do appreciate tips and advice if given.I am new to coding, currently taking a programming class. This is a personal project i thought would be fun.

I am working on a security program using a 4x4 matrix keypad w/RPi3. The keypad is not a membrane style, it is more like the old landline phones with white plastic buttons.

From what I can gather, based on what I have learned in class, i will also need an if/else loop or two.

Code:

import RPi.GPIO

GPIO.setmode(GPIO.BCM)

#Set Keypad Layout
Keypad = [
[1, 2, 3, "A"],
[4, 5, 6, "B"],
[7, 8, 9, "C"],
[0, "F", "E", "D"]
]
#Keypad -> GPIO Pin Out
#BCM Numbering
COL_PINS = [4, 17, 22, 27]
ROW_PINS = [18, 25, 5, 6]

#example keypad entered code
#CODE = A12345

factory = rpi_gpio.KeypadFactory()

# Try factory.create_4_by_3_keypad
# and factory.create_4_by_4_keypad for reasonable defaults
keypad = factory.create_keypad(keypad=KEYPAD, row_pins=ROW_PINS, col_pins=COL_PINS)

def printKey(key):
print(key)

# printKey will be called each time a keypad button is pressed
keypad.registerKeyPressHandler(printKey)

Code End
Doesn't really matter if it is a membrane or mechanical keypad, they both operate on the same principle. In a 4 x 4 keypad (4 columns x 4 rows) you have 16 normally open switches. One side of each switch is connected to a column the other to a row. You can the designate which represents what, for example, columns = input, rows = output. When pressed (say "1") you now have a "short" (most likely current flow) between column 1/row1; "2" would give you column 2/row 1 and so on. Your "if/elif/else" clauses when then have to say "if I have current flow from column 1 to row 1: the number "1" was pressed, otherwise if I have current flow between column 2 row 1: the number "2" was pressed, and so on.
So after a very long time of life events and what not, i was finally able to get around to tweaking and making my keypad code work. I am still a n00b at programming, but thanks to multiple forums i feel I am getting a good grasp of it.

Here is my current code:
import RPi.GPIO as GPIO

GPIO.setmode (GPIO.BOARD)

MATRIX = [[1,2,3,'A'],
          [4,5,6,'B'],
          [7,8,9,'C'],
          [0,'F','E','D']]

ROW = [7,11,13,15]
COL = [12,22,29,31]

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)

try:
    while(True):
        for j in range(4):
            GPIO.output(COL[j],0)

            for i in range(4):
                if GPIO.input(ROW[i]) == 0:
                    print MATRIX [i][j]
                    while(GPIO.input(ROW[i]) == 0):
                        pass
                    
            GPIO.output(COL[j],1)
except KeyboardInterrupt:
    GPIO.cleanup()
The current problem is that the output does not match the keypad layout, like so
Actual:
[1][2][3][A]
[4][5][6][B]
[7][8][9][C]
[0][F][E][D]

Output:
[1][4][7][0]
[2][5][8][F]
[3][6][9][E]
[A][B][C][D]

I am thinking that i somehow got the ROW and COL mixed? I couldnt find any pinout info for the keypad I am using and most I read about are all made the same way.

Any help would be appreciated.
garbage deleted, tumbleweeds collecting

no useful input.
deleted reply, thread gets no input.

have a good night/evening/day/morning.