Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Security Program Help
#1
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
Reply
#2
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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
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.
Reply
#4
garbage deleted, tumbleweeds collecting

no useful input.
Reply
#5
deleted reply, thread gets no input.

have a good night/evening/day/morning.
Reply


Forum Jump:

User Panel Messages

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