import machine
import time
from utime import sleep
BUTTON_PIN = 17
LED_PIN = 9
last_button_time_stamp = 0
button_pressed = False
# Extract the numeric pin id from the passed in Pin instance
def PinId(pin):
# Pin(17, mode=IN, pull=PULL_DOWN)
# the pin value is in the format above
# and the pin id, i.e. 7 or 17, can be single or double digit
# So, we filter the characters 8-11,
# which would be '7,' or '17' depending on the pin,
# then, we remove any ',' character at the end
# and we are left with the numeric value of the pin, i.e. 7 or 17
return int(str(pin)[8:11].rstrip(","))
def interrupt_callback(pin):
global last_button_time_stamp
global button_pressed
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
# Call the PinId method to get the numeric pin value
print(f'key press: {PinId(pin)}')
def main():
global last_button_time_stamp
global button_pressed
button_in = machine.Pin(BUTTON_PIN, machine.Pin.IN, machine.Pin.PULL_DOWN)
button_in.irq(trigger=machine.Pin.IRQ_FALLING, handler=interrupt_callback)
led = machine.Pin(LED_PIN, machine.Pin.OUT)
while True:
binary_code = 1
if button_pressed == True:
led.toggle()
print('Toggle the LED')
print('')
button_pressed = False
if __name__ == "__main__":
main()