Created
April 6, 2015 09:26
-
-
Save serverwentdown/f43d4ee4988c3dfa927a to your computer and use it in GitHub Desktop.
filled
This file contains hidden or 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
from time import * | |
import RPi.GPIO as GPIO | |
GPIO.setmode(GPIO.BOARD) # Use physical layout as reference pins | |
MATRIX = [[1, 2, 3, "A"], | |
[4, 5, 6, "B"], | |
[7, 8, 9, "C"], | |
["*", 0, "#", "D"]] | |
ROW = [13, 15, 19, 21] | |
COL = [12, 16, 18, 22] | |
GLED = 7 # Green LED | |
RLED = 11 # Red LED | |
# Setup LEDs | |
GPIO.setup(GLED, GPIO.OUT) | |
GPIO.setup(RLED, GPIO.OUT) | |
GPIO.output(GLED, False) | |
GPIO.output(RLED, False) | |
# Setup Matrix modes | |
for j in range(4): | |
GPIO.setup(COL[j], GPIO.OUT) | |
GPIO.output(COL[j], 1) | |
print(COL[j]) | |
for i in range(4): | |
GPIO.setup(ROW[i], GPIO.IN, pull_up_down = GPIO.PUD_UP) | |
print(ROW[i]) | |
inputpassword = "" | |
password = "2938" | |
try: | |
# Keep checking for keyboard events | |
while(True): | |
for j in range(4): | |
GPIO.output(COL[j], 0) | |
for i in range(4): | |
if GPIO.input(ROW[i]) is 0: | |
key = MATRIX[i][j] | |
print(MATRIX[i][j]) | |
if type(key) is int: | |
# Do stuff: | |
inputpassword += str(key) | |
# Blink green LED once: | |
GPIO.output(GLED, True) | |
sleep(0.25) | |
GPIO.output(GLED, False) | |
else: | |
if key is "D": | |
# Do stuff: | |
inputpassword = inputpassword[:-1] | |
# Blink red LED once: | |
GPIO.output(RLED, True) | |
sleep(0.25) | |
GPIO.output(RLED, False) | |
# When length is same as password's: | |
if len(inputpassword) == len(password): | |
# Check for equality: | |
if inputpassword == password: | |
# Make green light solid for 2 seconds: | |
GPIO.output(GLED, True) | |
else: | |
# Make red light solid for 2 seconds: | |
GPIO.output(RLED, True) | |
sleep(2) | |
GPIO.output(RLED, False) | |
GPIO.output(GLED, False) | |
# After that, clear inputpassword and reset lights: | |
inputpassword = "" | |
while GPIO.input(ROW[i]) is 0: | |
pass | |
GPIO.output(COL[j], 1) | |
except KeyboardInterrupt: | |
GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment