import machine
import time
from utime import sleep
from machine import Pin
s0 = machine.Pin(27, machine.Pin.OUT)
s1 = machine.Pin(28, machine.Pin.OUT)
mux_in = machine.Pin(26, machine.Pin.IN, machine.Pin.PULL_DOWN)
led_count = 9
led_start = 7
led_list = [Pin(led_start + i, Pin.OUT) for i in range(led_count)]
Pbutton_count = 3
Pbutton_start = 16
Pbutton_list = [Pin(Pbutton_start + i, Pin.IN, Pin.PULL_DOWN) for i in range(Pbutton_count)]
Pushed_Buttons = []
start_time = 0
correct_pass = [Pbutton_list[0], Pbutton_list[2], Pbutton_list[1]]
prev_binary_code = -1 # لتخزين آخر قيمة مطبوعة وتحديثها فقط عند التغيير
def read_binary_code():
binary_code = 0
for selector_val in range(4):
s0.value(selector_val % 2)
s1.value(selector_val // 2)
sleep(0.020)
binary_code += (pow(2, selector_val) * mux_in.value())
return binary_code
def interrupt(Pin):
global start_time
current_time = time.ticks_ms()
diff = current_time - start_time
if diff > 200:
button_index = Pbutton_list.index(Pin)
print(f'Button {button_index} pressed')
Pushed_Buttons.append(Pin)
start_time = current_time
check_passcode()
for pButton in Pbutton_list:
pButton.irq(trigger=Pin.IRQ_FALLING, handler=interrupt)
prev_binary_code = read_binary_code()
print(f'Initial Decimal representation: {prev_binary_code}')
def check_passcode():
global prev_binary_code
binary_code = read_binary_code()
if binary_code != prev_binary_code:
print(f'Decimal representation: {binary_code}')
prev_binary_code = binary_code
if len(Pushed_Buttons) >= len(correct_pass):
if Pushed_Buttons[:len(correct_pass)] == correct_pass:
print("Correct passcode")
if 0 <= binary_code < led_count:
led_list[binary_code].toggle()
print(f'Toggling LED at position {binary_code}')
else:
print(f'Selected output {binary_code} is out of range. Valid range: 0-{led_count - 1}')
else:
print("Incorrect passcode. Try again.")
Pushed_Buttons.clear()
while True:
binary_code = read_binary_code()
if binary_code != prev_binary_code:
print(f'Decimal representation: {binary_code}')
prev_binary_code = binary_code
sleep(0.1)