import machine
import time
from utime import sleep
time.sleep(0.1)
button_pin = machine.Pin(16,machine.Pin.IN,machine.Pin.PULL_DOWN)
button2_pin = machine.Pin(17,machine.Pin.IN,machine.Pin.PULL_DOWN)
button3_pin = machine.Pin(18,machine.Pin.IN,machine.Pin.PULL_DOWN)
button_pressed = False
last_button_time_stamp = 0
button_pressed = False
pressed_button_id = -1
object_pin_dict = {button_pin: 2 , button2_pin: 1 ,button3_pin: 0 }
s0 = machine.Pin(27,machine.Pin.OUT)
s1 = machine.Pin(28,machine.Pin.OUT)
mux_in = machine.Pin(26,machine.Pin.IN)
led_start_Pin= 7
num_leds = 9
def PinId(pin):
return int (str(pin)[8:11].rstrip(","))
def interrupt_callback(pin):
global last_button_time_stamp
global button_pressed
global pressed_button_id
cur_button_ts = time.ticks_ms()
button_press_delta = cur_button_ts - last_button_time_stamp
if button_press_delta > 200:
last_button_time_stamp = cur_button_ts
button_pressed = True
pressed_button_id = object_pin_dict[pin]
def read_switches():
binary_value = 0
selectors = [(0,0),(0,1),(1,0),(1,1)]
for selector_value in range(4):
s0.value(selectors[selector_value][0])
s1.value(selectors[selector_value][1])
binary_value += 2**selector_value*mux_in.value()
print (binary_value)
return binary_value
input_list = []
def timer_callback(t):
global input_list
if len(input_list)>0:
print('timer Triggered ,wrong password')
input_list.clear()
password =[2,0,1]
def main():
led_objects = []
for i in range(num_leds):
led = machine.Pin(led_start_Pin + i,machine.Pin.OUT)
led_objects.append(led)
input_list =[]
global button_pressed
global pressed_button_id
button_pin.irq(trigger = machine.Pin.IRQ_RISING,handler =interrupt_callback)
button2_pin.irq(trigger = machine.Pin.IRQ_RISING,handler =interrupt_callback)
button3_pin.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
if len(input_list) == 3:
if input_list == password:
print ('Enetred correct password ')
switch_val =read_switches()
if switch_val < num_leds:
led_objects[switch_val].toggle()
else:
print ('wrong password')
input_list.clear()
if __name__ == '__main__':
main()
# adpted from classroom(udacity)