-
Notifications
You must be signed in to change notification settings - Fork 0
/
keypadCode.py
181 lines (173 loc) · 7.29 KB
/
keypadCode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import RPi.GPIO as GPIO
import time as time
import servoCode
import doorcheck
import os
GPIO.setmode(GPIO.BOARD)
mat = [['1','2','3','A'],
['4','5','6','B'],
['7','8','9','C'],
['*','0','#','D']]
row = [7,11,13,15]
col = [12,16,18,32]
#pin = ['1','2','3','4']
pin='1234'
entered = ['0','0','0','0']
countEntered=0
countStandBy=0
MaxStandBy=50
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)
switchpin=37
GPIO.setup(switchpin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
passCorrectFlag=False
try:
while(True):
input_state = GPIO.input(switchpin)
if input_state == False:
print '\nOpening Lock!'
servoCode.turnmotor(-80)
doorcheck.checkWhenClosed()
countEntered=0
numEntered=False
for j in range(4):
GPIO.output(col[j],0)
for i in range(4):
if GPIO.input(row[i])==0:
numEntered=True
countEntered+=1
entered[countEntered-1]=mat[i][j]
if(i==0 and j==3):
print "Backspace"
countEntered=0
elif(i==3 and j==3):
print "Enter"
countEntered=0
elif(i==1 and j==3):
print "Add User"
countEntered=0
elif(i==2 and j==3):
print "Remove User"
countEntered=0
elif(i==3 and j==2):
print 'You wish to change pin!\n'
print 'Please enter the current pin!'
time.sleep(1.5)
os.system('clear')
keyentered='0'
countKey=0
keyid=[]
GPIO.output(col[j],1)
time.sleep(0.1)
while(keyentered!=mat[3][3] or countKey==0):
for q in range(4):
GPIO.output(col[q],0)
for w in range(4):
if GPIO.input(row[w])==0:
keyentered=mat[w][q]
if(keyentered=='D'):
break
elif(keyentered=='A'):
countKey-=1
os.system('clear')
else:
os.system('clear')
countKey+=1
if(countKey>len(keyid)):
keyid.append(keyentered)
else:
keyid[countKey-1]=keyentered
z=0
for z in range(countKey):
print keyid[z],
print ''
while (GPIO.input(row[w])==0):
pass
GPIO.output(col[q],1)
time.sleep(0.1)
p=len(keyid)-1
while(p>=countKey):
del keyid[p]
p-=1
keyid=''.join(keyid)
if keyid==pin:
print 'Pin verified !\n\nPlease enter the new pin!'
time.sleep(1.5)
os.system('clear')
keyentered='0'
countKey=0
keyid=[]
GPIO.output(col[j],1)
time.sleep(0.1)
while(keyentered!=mat[3][3] or countKey==0):
for q in range(4):
GPIO.output(col[q],0)
for w in range(4):
if GPIO.input(row[w])==0:
keyentered=mat[w][q]
if(keyentered=='D'):
break
elif(keyentered=='A'):
countKey-=1
os.system('clear')
elif(keyentered=='*' or keyentered=='#' or keyentered=='B' or keyentered=='C'):
print 'Please use only numbers!'
else:
os.system('clear')
countKey+=1
if(countKey>len(keyid)):
keyid.append(keyentered)
else:
keyid[countKey-1]=keyentered
z=0
for z in range(countKey):
print keyid[z],
print ''
while (GPIO.input(row[w])==0):
pass
GPIO.output(col[q],1)
time.sleep(0.1)
p=len(keyid)-1
while(p>=countKey):
del keyid[p]
p-=1
keyid=''.join(keyid)
pin = keyid
print 'Pin Changed!'
else:
print 'Old pin not correct!'
print mat[i][j],
while (GPIO.input(row[i])==0):
pass
GPIO.output(col[j],1)
if countEntered==4:
passCorrectFlag=True
countEntered=0
for k in range(4):
if pin[k]!=entered[k] :
passCorrectFlag=False
break
if passCorrectFlag :
print '\nCorrect Password!'
servoCode.turnmotor(-80)
doorcheck.checkWhenClosed()
else :
print '\nWrong Password!'
for k in range(4):
entered[k]='0'
if numEntered==True:
countStandBy=0
if numEntered==False:
countStandBy+=1
if (countStandBy > MaxStandBy and passCorrectFlag==True):
passCorrectFlag=False
countStandBy=0
for k in range(4):
entered[k]='0'
#servoCode.initMotor(-50)
time.sleep(0.1)
except KeyboardInterrupt:
GPIO.cleanup()