import machine
import time
from utime import sleep
## Pin interrupt handler
######################## The password is 201
# A function to handle pin interrupts
# Expects a machine.Pin type parameter
def interrupt_callback(pin):
global last_button_time_stamp, button_pressed, pressed_button_id
cur_button_ts = time.ticks_ms()
if cur_button_ts - last_button_time_stamp > DEBOUNCE_TIME:
last_button_time_stamp = cur_button_ts
button_pressed = True
pressed_button_id = buttons[pin]
def main():
sleep(0.01)
print('Program starting')
print('Hello try turning any one of the switches and then write the password on the toggle buttons the right one then the left one then the middle one [the pw order]')
PASSWORD = [2, 0, 1]
BUTTON_PINS = [16, 17, 18]
MUX_SELECT_PINS = [27, 28]
MUX_INPUT_PIN = 26
LED_START_PIN = 7
NUM_LEDS = 9
DEBOUNCE_TIME = 200
button_pressed = False
pressed_button_id = -1
last_button_time_stamp = 0
input_list = []
buttons = {machine.Pin(pin, machine.Pin.IN, machine.Pin.PULL_DOWN): idx for idx, pin in enumerate(BUTTON_PINS)}
s0, s1 = [machine.Pin(pin, machine.Pin.OUT) for pin in MUX_SELECT_PINS]
mux_in = machine.Pin(MUX_INPUT_PIN, machine.Pin.IN)
leds = [machine.Pin(LED_START_PIN + i, machine.Pin.OUT) for i in range(NUM_LEDS)]
def read_switches():
binary_value = 0
for selector_value in range(4):
s0.value(selector_value % 2)
s1.value(selector_value // 2)
binary_value += (2 ** selector_value) * mux_in.value()
return binary_value
def reset_timer(t):
global input_list
if input_list:
print("Timer Triggered: Wrong password")
input_list.clear()
def password201():
global button_pressed, pressed_button_id
last_binary_value = -1
for button in buttons:
button.irq(trigger=machine.Pin.IRQ_RISING, handler=interrupt_callback)
while True:
if button_pressed:
print("Key Pressed:", pressed_button_id)
input_list.append(pressed_button_id)
button_pressed = False
pressed_button_id = -1
switch_val = read_switches()
if switch_val != last_binary_value:
print("Switch Value:", switch_val)
last_binary_value = switch_val
if len(input_list) == len(PASSWORD):
if input_list == PASSWORD:
print("Entered Correct Password")
if 0 <= switch_val < NUM_LEDS:
leds[switch_val].toggle()
print("Switch Value:", switch_val)
else:
print("Invalid Input: ", switch_val, ", valid range: 0-8, doing nothing")
else:
print("Wrong Password")
input_list.clear()
time.sleep(0.05)
if __name__ == '__main__':
main()
if __name__ == "__main__":
password201()