# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# Activity 2 — Smart Safe Logic
# Level 3 | First Month Recap
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
from machine import Pin
import time
# ── Input switches ─────────────────────────────
# Using PULL_UP:
# Switch OFF/Open -> GPIO reads 1 -> check is False
# Switch ON/GND -> GPIO reads 0 -> check is True
master_key = Pin(12, Pin.IN, Pin.PULL_UP) # M
pin_code = Pin(13, Pin.IN, Pin.PULL_UP) # P
finger = Pin(14, Pin.IN, Pin.PULL_UP) # F
# ── Output LEDs ────────────────────────────────
unlocked_led = Pin(2, Pin.OUT) # Green LED: Safe Unlocked
locked_led = Pin(4, Pin.OUT) # Red LED: Safe Locked
last_state = None
while True:
# Read switches
# Because we use PULL_UP, pressed/active means GPIO value is 0
M = master_key.value() == 0
P = pin_code.value() == 0
F = finger.value() == 0
# Smart Safe Logic:
# Unlock if any 2 security checks are true
S = (M and F) or (P and F) or (M and P)
# Count how many checks are active
active_count = 0
if M:
active_count += 1
if P:
active_count += 1
if F:
active_count += 1
# Control LEDs
if S:
unlocked_led.value(1)
locked_led.value(0)
else:
unlocked_led.value(0)
locked_led.value(1)
# Print only when state changes to avoid too many messages
current_state = (M, P, F, S)
if current_state != last_state:
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
# Option A: Print active security checks
print("Active Security Checks:")
if M:
print("- Master Key inserted")
if P:
print("- Correct PIN entered")
if F:
print("- Fingerprint matched")
if active_count == 0:
print("- None")
# Print safe result
if S:
print("Safe Status: UNLOCKED")
else:
print("Safe Status: LOCKED")
# Option C: Warning if only one check is true
if active_count == 1:
print("Warning: Only one security check is active!")
print("The safe needs at least 2 checks to unlock.")
last_state = current_state
time.sleep(0.1)