Neither does anyone else....
Code: Select all
Like this
Code: Select all
dir
Code: Select all
sudo python Keypad2.py
Code: Select all
'E' = 'Enter'
Code: Select all
import RPi.GPIO as GPIO
import time
GPIO.setmode (GPIO.BOARD)
MATRIX = [[1,2,3,'A'],
[4,5,6,'B'],
[7,8,9,'C'],
[0,'F','E','D']]
COL = [7,11,13,15]
ROW = [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:
time.sleep(0.02)
print MATRIX [i][j]
while(GPIO.input(ROW[i]) == 0):
pass
GPIO.output(COL[j],1)
except KeyboardInterrupt:
GPIO.cleanup()
Code: Select all
import time
import Keypad2
choice = input
choice = int(choice)
if choice == 1:
User1()
elif choice == 2:
User2()
def User1():
time.sleep(1)
# Password verification code goes here
if input == "1234ABCD"
print("Welcome User1!")
else:
print("Access Denied!!")
def User2():
time.sleep(1)
# Password verification code goes here
if input == "5678EFGH"
print("Welcome User2!")
else:
print("Access Denied!!")
Code: Select all
try:
while(True):
for j in range(4):
GPIO.output(COL[j],0)
for i in range(4):
if GPIO.input(ROW[i]) == 0:
time.sleep(0.02)
print MATRIX [i][j]
while(GPIO.input(ROW[i]) == 0):
time.sleep(0.02)
GPIO.output(COL[j],1)
Code: Select all
import time
#import Keypad2
choice = raw_input("Make Selection: 1-2!")
while(choice != '1') and (choice != '2'):
choice = raw_input("Make Selection: 1-2!")
if choice == '1':
User1()
elif choice == '2':
User2()
def User1():
print("Welcome")
while True:
PassCode == raw_input("Please Enter Code: ")
if PassCode == 'A12345':
time.sleep(1)
print("Welcome!!")
Code: Select all
Traceback (most recent call last):
File: "Power-On.py", line 15, in <module>
User1()
NameError: name 'User1()' is not defined
Yes you have to define the function in the code before the line were you are going to call it.
Thats something ill have to get used to lol. I started learning programming using C++ and its slightly different.So the order I would write a simple program would be.
imports
set up gpio
set variables
def functions
then main loop or main program code
Code: Select all
import RPi.GPIO as GPIO
import time
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)
passcode = 'A12345'
attempt = ' '
try:
while (True):
for j in range(4):
GPIO.output(COL[j], 0)
for i in range(4):
if GPIO.input(ROW[i]) == 0:
time.sleep(0.01)
while (GPIO.input(ROW[i]) == 0):
pass
attempt += MATRIX[i][j]
if len(attempt) == len(passcode):
if attempt == passcode:
print("Welcome User")
print("Ta-da!!!")
else:
print("Denied!!")
print("Try Again!!")
attempt = ""
time.sleep(0.01)
GPIO.output(COL[j], 1)
except KeyboardInterrupt:
GPIO.cleanup()
Code: Select all
passcode = 'A12345'
attempt = ' '
attempt += MATRIX[i][j]
if len(attempt) == len(passcode):
if attempt == passcode:
print("Welcome User")
print("Ta-da!!!")
else:
print("Denied!!")
print("Try Again!!")
attempt = ""
time.sleep(0.01)
GPIO.output(COL[j], 1)
Code: Select all
import time
#import Keypad2
choice = raw_input("Make Selection: 1-2!")
while(choice != '1') and (choice != '2'):
choice = raw_input("Make Selection: 1-2!")
if choice == '1':
User1()
elif choice == '2':
User2()
def User1():
print("Welcome")
while True:
PassCode = raw_input("Please Enter Code: ")
if PassCode == 'A12345':
attempt = ' '
attempt += MATRIX[i][j]
if len(attempt) == len(PassCode):
if attempt == PassCode:
time.sleep(1)
print("Welcome!!")
else:
print("Denied!!")
print("Try Again!!")
attempt == ' '
time.sleep(0.02)
GPIO.output(COL[j], 1)
Code: Select all
NameError: global name 'MATRIX' is not defined
The only reference to your keypad module is the commented out lineStangPi wrote: ↑Sun Mar 04, 2018 6:47 pmfirst attempt did not work:Code: Select all
import time #import Keypad2 choice = raw_input("Make Selection: 1-2!") while(choice != '1') and (choice != '2'): choice = raw_input("Make Selection: 1-2!") if choice == '1': User1() elif choice == '2': User2() def User1(): print("Welcome") while True: PassCode = raw_input("Please Enter Code: ") if PassCode == 'A12345': attempt = ' ' attempt += MATRIX[i][j] if len(attempt) == len(PassCode): if attempt == PassCode: time.sleep(1) print("Welcome!!") else: print("Denied!!") print("Try Again!!") attempt == ' ' time.sleep(0.02) GPIO.output(COL[j], 1)
It gives me the error:Im assuming that the import keypad script isn't properly linking with the security script.Code: Select all
NameError: global name 'MATRIX' is not defined
Code: Select all
#import Keypad2
What would be the best way to do so? I'm still getting accustomed to how Python works.I'd say put the code in your Keypad2.py into functions (otherwise the code will run when imported rather than when you want it to), then when you want to use those functions in other files you can import the module and call the relevant functions. So you could have three functions, one to initialise stuff (especially things that don't want initialising at the moment of import), one that gets input from the keypad (either returning a single key or a string of keys depending on what you want) and one that closes things down (if anything).
Code: Select all
import RPi.GPIO as GPIO
import time
# GPIO setup
GPIO.setmode (GPIO.BOARD)
GPIO.setwarnings(False)
COL = [7,11,13,15]
ROW = [12,22,29,31]
for j in range(4):
GPIO.setup(COL[j], GPIO.OUT)
GPIO.output (COL[j], 1)
GPIO.setup(ROW[j], GPIO.IN, pull_up_down = GPIO.PUD_UP)
# function for checking keypad input
def check_keypad(length):
COL = [7,11,13,15]
ROW = [12,22,29,31]
MATRIX = [["1","2","3","A"],
["4","5","6","B"],
["7","8","9","C"],
["0","F","E","D"]]
result = ""
while(True):
for j in range(4):
GPIO.output(COL[j],0)
for i in range(4):
if GPIO.input(ROW[i]) == 0:
time.sleep(0.02)
result = result + MATRIX [i][j]
while(GPIO.input(ROW[i]) == 0):
time.sleep(0.02)
GPIO.output(COL[j],1)
if len(result) >= length:
return result
# select user
print ("select user number on keypad")
result = check_keypad(1)
user = "user" + result
print ("welcome ",user)
# select user password
# user1 password
if user == "user1":
password = "12345"
length = len(password)
# user2 password
if user == "user2":
password = "123456"
length = len(password)
# get password from keypad
print ("enter password on key pad")
result = check_keypad(length)
# check password
if result == password:
print ("Password correct welcome ",user)
else:
print("Error Password does not match")
Code: Select all
# GPIO setup GPIO.setmode (GPIO.BOARD) GPIO.setwarnings(False) COL = [7,11,13,15] ROW = [12,22,29,31] for j in range(4): GPIO.setup(COL[j], GPIO.OUT) GPIO.output (COL[j], 1) GPIO.setup(ROW[j], GPIO.IN, pull_up_down = GPIO.PUD_UP) # function for checking keypad input def check_keypad(length): COL = [7,11,13,15] ROW = [12,22,29,31] MATRIX = [["1","2","3","A"], ["4","5","6","B"], ["7","8","9","C"], ["0","F","E","D"]] result = "" while(True): for j in range(4): GPIO.output(COL[j],0) for i in range(4): if GPIO.input(ROW[i]) == 0: time.sleep(0.02) result = result + MATRIX [i][j] while(GPIO.input(ROW[i]) == 0): time.sleep(0.02) GPIO.output(COL[j],1) if len(result) >= length: return result
By default the RPi.GPIO module will warn you if, when you set a GPIO's function, the GPIO in question wasn't in IN or OUT mode (or it was in OUT mode but not set to that by the current program) e.g. when another program is also using the GPIO and has changed it. GPIO.setwarnings(False) disables that warning.
It is doing the same as when you wrote attempt += MATRIX[i][j], just adding the element from MATRIX[i][j] to the end of the string result
Once the length of the code being input reaches the length that was passed in to the function it returns the whole code back to the caller, it's then up to the caller to do what they want with the code (in this case the main code goes on to check the returned value against a valid code code).
some times its just easier to write an example so that poeple can see how to do the layout that actually trying to explain it. As the old saying goes a picture is worth a thousand words.I wasn't expecting a full written code, thanks you. Will definitely help me to understand python and complete my project.
A few things:
what GPIO.setwarnings(False) does is suppress the warning message that the GPIO is already in use on the second, third and so no runs of the program , if you just comment out the line adding # as the first character in the line in question then run the program twice on the second execution you will see the warning messages.1--This code is to identify the GPIO connections and functions. What does the GPIO.setwarnings do? Im guessing what its named for, but i mean how so?
This line adds each key press to the variable result, so for example when entering the password result starts empty2--> result = result + MATRIX [ i ][ j ]<--is this checking the input and checking it against the MATRIX layout?
yes that checks the length of the entered code so for user1 the length would be 5 characters but for user2 it would be 6 characters3--> if len(result) >= length: <--Does this determine when the password has been completely entered before checking its validity? Or is that bit within the user password input blocks?
Code: Select all
MATRIX = [["1","2","3","A"],
["4","5","6","B"],
["7","8","9","C"],
["0","F","E","D"]]
Not really, other than if you want to use an apostrophe or a quote in the string itself then you use the other for the delimiter. If you used an apostrophe as a delimiter and want an apostrophe in the string you would have to escape the one inside the string, and the same with a quote. E.g. these are equivalent, both giving the string You're :-StangPi wrote: ↑Tue Mar 06, 2018 2:47 amIs there a particular difference between using apostrophes( ' ), or quotations( " " ) ?Code: Select all
MATRIX = [["1","2","3","A"], ["4","5","6","B"], ["7","8","9","C"], ["0","F","E","D"]]
Code: Select all
"You're"
'You\'re'
if user == "user2":
password = "123456"
length = len(password)
# get password from keypad
print ("enter password on key pad")
result = check_keypad(length)
# check password
if result == password:
print ("Password correct welcome ",user)
else:
print("Error Password does not match")
Code: Select all
if User1 == "User1":
password = "A1234"
length = len(password)
if result == password:
print("Welcome!!")
#(Function to take User1 to whatever is being protected goes here)
elif result != password:
print("Denied!!")