from machine import Pin #import libraries
import utime
#this is the list of pins connected to leds
left_led_list = [Pin(i, Pin.OUT) for i in range(3,11)]
right_led_list=left_led_list[::-1]
buttonleft = Pin(1, Pin.IN, Pin.PULL_UP)
buttonleft.irq(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=debounce_button)
buttonright = Pin(2, Pin.IN, Pin.PULL_UP)
buttonright.irq(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=debounce_button)
for led in left_led_list:
led.value(0)#to ensure that at the first state all the leds are off, we set output pin voltages of the leds to 0.
def debounce_button(pin):
"""this method is used to handle debounce, checks state between 25 ms"""
utime.sleep(0.1) #this code helps pi pico to stabilize the values coming from the button while interruption
button = pin
global old_button_state, button_down_time, button_up_time, new_button_state
if button.value() == 0:
button_down_time = utime.ticks_ms()
if button.value() == 1:
button_up_time = utime.ticks_ms()
if old_button_state == 1 and button.value() == 0 and utime.ticks_diff(button_down_time, button_up_time) > 25: #We check the difference time between the time that is pressed the button and released the button, and then we make sure that time is bigger than 25 ms so that pi pico can take the input from the button without debouncing.
new_button_state *= -1 #we ceheck the button state by this variable
old_button_state = button.value() #the old button state is preserved here.
utime.sleep(0.1)
def game():
if buttonright.value():
for led in right_led_list:
led.value(1)
utime.sleep(1)
led.value(0)
if buttonleft.value():
for led in right_led_list:
led.value(1)
utime.sleep(1)
led.value(0)