CookieGamez2018
Posts: 6
Joined: Sun Dec 09, 2018 9:22 pm

Keypad Passcode

Sun Dec 09, 2018 9:36 pm

Hello,
i need some help with setting up a pass-code with my raspberry pi. i would like it to be a four digit code that you enter on the keypad and it prints "access granted" if right and "access denied" if wrong. any help would be great!

my code so far, it prints the button you hit:

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

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

ROW = [3,5,8,10]
COL = [19,21,23,24]

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])
                    time.sleep(0.2)
                    while(GPIO.input(ROW[i]) == 0):
                        pass


            GPIO.output(COL[j],1)    
except KeyboardInterupt:
    GPIO.cleanup()
    
Last edited by CookieGamez2018 on Thu Dec 13, 2018 12:13 am, edited 3 times in total.

Andyroo
Posts: 4233
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Keypad Passcode

Mon Dec 10, 2018 11:42 am

Welcome to the forum.

What keypad do you have?

Could please edit your post so the code is in the 'code' tag:
[ c o d e ] Python goes here [ / c o d e ]
(without the spaces by using the </> button>)
Then we can see the indents in the program - these are vital for Python :lol:
Need Pi spray - these things are breeding in my house...

scotty101
Posts: 3682
Joined: Fri Jun 08, 2012 6:03 pm

Re: Keypad Passcode

Mon Dec 10, 2018 11:57 am

CookieGamez2018 wrote:
Sun Dec 09, 2018 9:36 pm

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

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

ROW = [3,5,8,10]
COL = [19,21,23,24]

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])
                    time.sleep(0.2)
                    while(GPIO.input(ROW[i]) == 0):
                        pass


            GPIO.output(COL[j],1)    
except KeyboardInterupt:
    GPIO.cleanup()
Does your code currently work? As in does it print out the correct key when you press a button?

If so, all you need to do is add each key pressed to a list and compare the contents of the list to another list that contains the correct password.

Might look something like

Code: Select all

#Start with a blank list
userEntry = []
# Define the correct answer
correctKey = [1,2,3,4]
...........
#Add the pressed key to the list
userEntry.append(MATRIX[i][j])
#compare the length of the list of entered keys against the correct answer
if len(userEntry) == len(correctKey):
    #If they match, check to see if the value is correct
    if userEntry == correctKey:
        print("Correct")
    else:
        print("Incorrect!!")
    #Clear the list for next time.
    userEntry.clear()
        
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Andyroo
Posts: 4233
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Keypad Passcode

Mon Dec 10, 2018 12:02 pm

@scotty101 - I hope your indents match his as that could be part of his problem :lol: :mrgreen:
Need Pi spray - these things are breeding in my house...

scotty101
Posts: 3682
Joined: Fri Jun 08, 2012 6:03 pm

Re: Keypad Passcode

Mon Dec 10, 2018 12:17 pm

Andyroo wrote:
Mon Dec 10, 2018 12:02 pm
@scotty101 - I hope your indents match his as that could be part of his problem :lol: :mrgreen:
Not sure what you mean.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Andyroo
Posts: 4233
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Keypad Passcode

Mon Dec 10, 2018 7:29 pm

scotty101 wrote:
Mon Dec 10, 2018 12:17 pm
Andyroo wrote:
Mon Dec 10, 2018 12:02 pm
@scotty101 - I hope your indents match his as that could be part of his problem :lol: :mrgreen:
Not sure what you mean.
Apologies - spent part of the weekend sorting a working program where my indentation did not match the original and that was the issue - only when it was posted in code blocks did I find it :oops:
Need Pi spray - these things are breeding in my house...

CookieGamez2018
Posts: 6
Joined: Sun Dec 09, 2018 9:22 pm

Re: Keypad Passcode

Wed Dec 12, 2018 6:54 pm

scotty101 wrote:
Mon Dec 10, 2018 11:57 am
CookieGamez2018 wrote:
Sun Dec 09, 2018 9:36 pm

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

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

ROW = [3,5,8,10]
COL = [19,21,23,24]

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])
                    time.sleep(0.2)
                    while(GPIO.input(ROW[i]) == 0):
                        pass


            GPIO.output(COL[j],1)    
except KeyboardInterupt:
    GPIO.cleanup()
Does your code currently work? As in does it print out the correct key when you press a button?

If so, all you need to do is add each key pressed to a list and compare the contents of the list to another list that contains the correct password.

Might look something like

Code: Select all

#Start with a blank list
userEntry = []
# Define the correct answer
correctKey = [1,2,3,4]
...........
#Add the pressed key to the list
userEntry.append(MATRIX[i][j])
#compare the length of the list of entered keys against the correct answer
if len(userEntry) == len(correctKey):
    #If they match, check to see if the value is correct
    if userEntry == correctKey:
        print("Correct")
    else:
        print("Incorrect!!")
    #Clear the list for next time.
    userEntry.clear()
        
Yes it does print the correct key that I press. also where would i put the code that you provided
Last edited by CookieGamez2018 on Thu Dec 13, 2018 1:49 am, edited 1 time in total.

CookieGamez2018
Posts: 6
Joined: Sun Dec 09, 2018 9:22 pm

Re: Keypad Passcode

Thu Dec 13, 2018 12:13 am

Andyroo wrote:
Mon Dec 10, 2018 11:42 am
Welcome to the forum.

What keypad do you have?

Could please edit your post so the code is in the 'code' tag:
[ c o d e ] Python goes here [ / c o d e ]
(without the spaces by using the </> button>)
Then we can see the indents in the program - these are vital for Python :lol:
i am using a 4x4 keypad

scotty101
Posts: 3682
Joined: Fri Jun 08, 2012 6:03 pm

Re: Keypad Passcode

Thu Dec 13, 2018 9:43 am

Did you try using the code example I provided?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

CookieGamez2018
Posts: 6
Joined: Sun Dec 09, 2018 9:22 pm

Re: Keypad Passcode

Thu Dec 13, 2018 9:02 pm

scotty101 wrote:
Thu Dec 13, 2018 9:43 am
Did you try using the code example I provided?
Yes it did work thank you so much

CookieGamez2018
Posts: 6
Joined: Sun Dec 09, 2018 9:22 pm

Re: Keypad Passcode

Thu Dec 13, 2018 9:04 pm

Thank you Scotty101 so much for helping me and solving the problem :D

Return to “Python”