Page 1 of 1

Keypad Passcode

Posted: Sun Dec 09, 2018 9:36 pm
by CookieGamez2018
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()
    

Re: Keypad Passcode

Posted: Mon Dec 10, 2018 11:42 am
by Andyroo
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:

Re: Keypad Passcode

Posted: Mon Dec 10, 2018 11:57 am
by scotty101
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()
        

Re: Keypad Passcode

Posted: Mon Dec 10, 2018 12:02 pm
by Andyroo
@scotty101 - I hope your indents match his as that could be part of his problem :lol: :mrgreen:

Re: Keypad Passcode

Posted: Mon Dec 10, 2018 12:17 pm
by scotty101
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.

Re: Keypad Passcode

Posted: Mon Dec 10, 2018 7:29 pm
by Andyroo
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:

Re: Keypad Passcode

Posted: Wed Dec 12, 2018 6:54 pm
by CookieGamez2018
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

Re: Keypad Passcode

Posted: Thu Dec 13, 2018 12:13 am
by CookieGamez2018
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

Re: Keypad Passcode

Posted: Thu Dec 13, 2018 9:43 am
by scotty101
Did you try using the code example I provided?

Re: Keypad Passcode

Posted: Thu Dec 13, 2018 9:02 pm
by CookieGamez2018
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

Re: Keypad Passcode

Posted: Thu Dec 13, 2018 9:04 pm
by CookieGamez2018
Thank you Scotty101 so much for helping me and solving the problem :D