from machine import Pin
from gpio_lcd import GpioLcd
from time import sleep
lcd = GpioLcd(
rs_pin=Pin(0),
enable_pin=Pin(1),
d4_pin=Pin(2),
d5_pin=Pin(3),
d6_pin=Pin(4),
d7_pin=Pin(5),
num_lines=2,
num_columns=16,
)
green = Pin(13, Pin.OUT, value=0)
red = Pin(14, Pin.OUT, value=0)
button = Pin(15, Pin.OUT, Pin.PULL_UP)
segments = [
Pin(6, Pin.OUT, value=0),
Pin(7, Pin.OUT, value=0),
Pin(8, Pin.OUT, value=0),
Pin(9, Pin.OUT, value=0),
Pin(10, Pin.OUT, value=0),
Pin(11, Pin.OUT, value=0),
Pin(12, Pin.OUT, value=0),
]
number_pattens = [
"1111110",
"0110000",
"1101101",
"1111001",
"0110011",
"1011011",
"1011111",
"1110000",
"1111111",
"1111011",
]
def show_message(top, bottom):
lcd.clear()
lcd.putstr(top)
lcd.move_to(0, 1)
lcd.putstr(bottom)
def show_number(number):
for index in range(7):
segments[index].value(int(number_pattens[number][index]))
def wait_for_button():
while button.value() == 0:
sleep(0.01)
sleep(0.05)
while button.value() == 1:
sleep(0.01)
sleep(0.05)
while button.value() == 0:
sleep(0.01)
sleep(0.05)
def flash_alarm():
show_message("COOKIE THIEF", "VAULT LOCKED!")
print("ALARM! Keep your paws off the cookie!")
for flash in range(6):
red.value(1)
sleep(0.2)
red.value(0)
sleep(0.2)
red.value(1)
secret_password = "cookie"
while True:
tries = 3
green.value(0)
red.value(0)
show_number(tries)
show_message("COOKIE VAULT", "3 tries to open!")
print("\nCOOKIE VAULT - Protect the last biscuit!")
print("Type a password, pres Enter, then click the button.")
sleep(1)
while tries > 0:
show_message("Enter password", "Serial Monitor")
password = input("\n Enter password:").lower()
show_message("PASSWORD READY", "Press button")
print("Click and release the button to check.")
#wait_for_button()
if password == secret_password:
green.value(1)
show_message("VAULT OPEN!", "Cookie is yours!")
print("ACCESS GRANTED! The cookie has a new owner!")
break
else:
tries = tries - 1
show_number(tries)
print("Wrong password! Tries left:" ,tries)
if tries == 0:
flash_alarm()
elif tries == 1:
show_message("LAST TRY!", "Don't panic!")
else:
show_message("WRONG PASSWORD!", "Tries left:" + str(tries))
if tries > 0:
red.value(1)
sleep(1)
red.value(0)
again = input("\n Play again? yes/no:").lower()
if again == "yes":
continue
else:
green.value(0)
red.value(0)
show_number(0)
show_message("VAULT OFFLINE", "Goodbye, human!")
print("Cookie security is off duty. Goodbye!")
break