from machine import Pin, PWM
from utime import sleep_ms
# ========== PIN SETUP ==========
btns = [
Pin(5, Pin.IN, Pin.PULL_UP),
Pin(4, Pin.IN, Pin.PULL_UP),
Pin(3, Pin.IN, Pin.PULL_UP),
Pin(2, Pin.IN, Pin.PULL_UP)
]
green_led = Pin(15, Pin.OUT)
red_led = Pin(0, Pin.OUT)
buzzer = PWM(Pin(1))
buzzer.duty_u16(0)
# ========== SYSTEM VARIABLES ==========
Correct_Password = [0, 1, 2, 3]
Entered_Password = []
Failed_Attempts = 0
Max_Attempts = 3
def tone(freq, duration):
buzzer.freq(freq)
buzzer.duty_u16(32768)
sleep_ms(duration)
buzzer.duty_u16(0)
print("--- System Ready! Please enter 4-digit code ---")
while True:
for i in range(4):
if btns[i].value() == 0:
tone(400, 100)
Entered_Password.append(i)
print(f"Entered: {Entered_Password}")
sleep_ms(20)
if len(Entered_Password) == 4:
#print("\nEvaluating Code: ****")
pass
if Entered_Password == Correct_Password:
print(">> Access Granted! Welcome. <<\n")
green_led.value(1)
tone(1000, 400)
sleep_ms(1000)
green_led.value(0)
Failed_Attempts = 0
else:
Failed_Attempts += 1
print(f">> Wrong Code! Attempts left: {Max_Attempts - Failed_Attempts} <<\n")
red_led.value(1)
tone(200, 500)
sleep_ms(500)
red_led.value(0)
Entered_Password = []
if Failed_Attempts >= Max_Attempts:
print("!!! SECURITY LOCKOUT: Too many failed attempts !!!\n")
red_led.value(1)
for _ in range(5):
tone(2500, 150)
sleep_ms(100)
red_led.value(0)
Failed_Attempts = 0
Entered_Password = []
print("--- System Reset. Try again ---")
sleep_ms(20)