Created
June 15, 2015 23:31
-
-
Save scottx611x/cf54a1099a6265d1523e to your computer and use it in GitHub Desktop.
Verify User Knock Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pifacedigitalio as pfio | |
import time,sys, threading, pickle | |
pifacedigital = pfio.PiFaceDigital() | |
pfio.init() | |
#Solenoid LOW | |
pfio.digital_write(5, 1) | |
time.sleep(.1) | |
count = 0 | |
f = open("KNOCKS","r") | |
CORRECT_CODE = pickle.load(f) | |
print "Enter code to open door. Ctrl+C to SAVE and EXIT: " | |
KNOCKS = {} | |
TIMES = [] | |
while True: | |
try: | |
#Check for action on the Piezo Pad | |
knock = pfio.digital_read(4,0) | |
#if a knock occurs.... | |
if knock == 1: | |
if count == 0: | |
start = time.time() | |
kn = "KNOCK%d" % count | |
print kn + "\n" | |
#ADD TIME TO ARRAY | |
TIMES.append(start) | |
KNOCKS[kn] = TIMES[count] | |
count = count + 1 | |
else: | |
kn = "KNOCK%d" % count | |
print kn + "\n" | |
#GET TIME OF KNOCK | |
t = time.time() - start | |
#ADD TIME TO ARRAY | |
TIMES.append(t) | |
KNOCKS[kn] = TIMES[count] | |
count = count + 1 | |
#GREEN LED ON | |
pfio.digital_write(6, 1) | |
#RED LED OFF | |
pfio.digital_write(7, 0) | |
time.sleep(.05) | |
else: | |
#RED LED ON | |
pfio.digital_write(7, 1) | |
#GREEN LED OFF | |
pfio.digital_write(6, 0) | |
except KeyboardInterrupt: | |
#CHECK KNOCK AGAINST ORIGINAL (CORRECT_CODE) | |
#check_count == 1 to elimate checking initial time of first knock | |
print "USER CODE IS: \n" | |
print CORRECT_CODE, "\n" | |
print "CODE TO CHECK IS: \n" | |
print KNOCKS, "\n" | |
check_count = 1 | |
PASS = 1 | |
CHECK_CODE = KNOCKS | |
print "CHECKING NUMBER OF KNOCKS\n" | |
time.sleep(1) | |
if len(CORRECT_CODE) == len(CHECK_CODE): | |
print "Number of knock is correct\n" | |
else: | |
print "Number of knock is incorrect :(\n" | |
PASS = 0 | |
print "CHECKING TIME BETWEEN KNOCKS" | |
time.sleep(1) | |
while check_count < len(CHECK_CODE): | |
if (CHECK_CODE["KNOCK%d" % check_count] < CORRECT_CODE["KNOCK%d" % check_count] + .25): | |
print "KNOCK%d LESS THAN UPPER BOUNDS\n" % check_count | |
if (CHECK_CODE["KNOCK%d" % check_count] > CORRECT_CODE["KNOCK%d" % check_count] - .25): | |
print "KNOCK%d GREATER THAN LOWER BOUNDS\n" % check_count | |
else: | |
print "KNOCK%d LESS THAN LOWER BOUNDS :( \n" % check_count | |
PASS = 0 | |
else: | |
print "KNOCK%d GREATER THAN UPPER BOUNDS :( \n" % check_count | |
PASS = 0 | |
check_count += 1 | |
if PASS: | |
print "YOU HAVE KNOCKED YOUR CODE SUCCESSFULLY!!!! :D\n" | |
print "OPENING DOOR LOCK FOR 5 SECONDS.....\n" | |
#Solenoid ON | |
pfio.digital_write(5, 0) | |
time.sleep(5) | |
#Solenoid OFF | |
pfio.digital_write(5, 1) | |
time.sleep(2) | |
#General Cleanup | |
pfio.deinit() | |
sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment