Skip to content

Instantly share code, notes, and snippets.

@scottx611x
Created June 15, 2015 23:30
Show Gist options
  • Save scottx611x/35485e7eacd77712df3d to your computer and use it in GitHub Desktop.
Save scottx611x/35485e7eacd77712df3d to your computer and use it in GitHub Desktop.
Get User Knock data
import pifacedigitalio as pfio
import time,sys, threading, pickle
pifacedigital = pfio.PiFaceDigital()
pfio.init()
#Blink LED to let user know they can enter knock codes
def blink_LED(PIN):
while 1:
p = PIN
pfio.digital_write(p, 1)
time.sleep(.1)
pfio.digital_write(p, 0)
time.sleep(.1)
count = 0
#Blue LED
PIN = 4
#Blink Blue LED in a thread with its own timer
t = threading.Thread(target=blink_LED, args=(PIN,))
t.start()
#Solenoid LOW
pfio.digital_write(5, 1)
time.sleep(.1)
print "DEVICE READY\n"
print "Please knock ONCE to enter a new code: "
one_knock = 0
while one_knock != 1:
#Check for action on the Piezo Pad
knock = pfio.digital_read(4,0)
if knock == 1:
one_knock = one_knock + 1
print "KNOCK acknowledged!"
time.sleep(2)
print "Please enter your secret knock code. Ctrl+C to SAVE and EXIT: "
KNOCKS = {}
TIMES = []
with open("KNOCKS", "w") as K:
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:
#Write knock code dict to file
pickle.dump(KNOCKS, K)
#General Cleanup
pfio.deinit()
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment