|
| 1 | +# swith status with no debounce logic |
| 2 | +from machine import Pin |
| 3 | +from utime import sleep, ticks_ms |
| 4 | + |
| 5 | +builtin_led = Pin(25) |
| 6 | +# Upper left swith |
| 7 | +UL_PIN = 2 |
| 8 | +LL_PIN = 3 |
| 9 | +UR_PIN = 14 |
| 10 | +LR_PIN = 13 |
| 11 | + |
| 12 | +ul_switch = Pin(UL_PIN, Pin.IN, Pin.PULL_UP) |
| 13 | +ll_switch = Pin(LL_PIN, Pin.IN, Pin.PULL_UP) |
| 14 | +ur_switch = Pin(UR_PIN, Pin.IN, Pin.PULL_UP) |
| 15 | +lr_switch = Pin(LR_PIN, Pin.IN, Pin.PULL_UP) |
| 16 | + |
| 17 | +# get current values as global variables |
| 18 | +ul = ul_switch.value() |
| 19 | +ll = ll_switch.value() |
| 20 | +ur = ur_switch.value() |
| 21 | +lr = lr_switch.value() |
| 22 | + |
| 23 | +# old values |
| 24 | +ul_old = 0 |
| 25 | +ll_old = 0 |
| 26 | +ur_old = 0 |
| 27 | +lr_old = 0 |
| 28 | + |
| 29 | +last_time = 0 |
| 30 | +switch_counter = 0 |
| 31 | +# this function gets called every time the button is pressed |
| 32 | +def switch_changed_handler(pin): |
| 33 | + global last_time, switch_counter, ul, ll, ur, lr |
| 34 | + new_time = ticks_ms() |
| 35 | + # if it has been more that 1/5 of a second since the last event, we have a new event |
| 36 | + if (new_time - last_time) > 200: |
| 37 | + if 'GPIO2' in str(pin): |
| 38 | + if ul == 0: |
| 39 | + ul = 1 |
| 40 | + else: ul = 0 |
| 41 | + if 'GPIO3' in str(pin): |
| 42 | + if ll == 0: |
| 43 | + ll = 1 |
| 44 | + else: ll = 0 |
| 45 | + if 'GPIO14' in str(pin): |
| 46 | + if ur == 0: |
| 47 | + ur = 1 |
| 48 | + else: ur = 0 |
| 49 | + if 'GPIO13' in str(pin): |
| 50 | + if lr == 0: |
| 51 | + lr = 1 |
| 52 | + else: lr = 0 |
| 53 | + switch_counter +=1 |
| 54 | + last_time = new_time |
| 55 | + |
| 56 | +# now we register the handler function when the button is pressed |
| 57 | +# https://docs.micropython.org/en/latest/library/machine.Pin.html |
| 58 | +ul_switch.irq(trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING), handler = switch_changed_handler) |
| 59 | +ll_switch.irq(trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING), handler = switch_changed_handler) |
| 60 | +ur_switch.irq(trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING), handler = switch_changed_handler) |
| 61 | +lr_switch.irq(trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING), handler = switch_changed_handler) |
| 62 | + |
| 63 | +old_switch_counter = 0 |
| 64 | +counter = 0 |
| 65 | +print('Startup:', ul, ll, ur, lr) |
| 66 | +while True: |
| 67 | + # only print on change in the button_presses value |
| 68 | + if old_switch_counter != switch_counter: |
| 69 | + # print('Switch counter: ', switch_counter) |
| 70 | + old_switch_counter = switch_counter |
| 71 | + |
| 72 | + if ul != ul_old: |
| 73 | + if ul == 0: |
| 74 | + print("Upper Left = On") |
| 75 | + else: print("Upper Left = Off") |
| 76 | + builtin_led.toggle() |
| 77 | + ul_old = ul |
| 78 | + counter += 1 |
| 79 | + |
| 80 | + if ll != ll_old: |
| 81 | + if ll == 0: |
| 82 | + print("Lower Left = On") |
| 83 | + else: print("Lower Left = Off") |
| 84 | + builtin_led.toggle() |
| 85 | + ll_old = ll |
| 86 | + counter += 1 |
| 87 | + |
| 88 | + if ur != ur_old: |
| 89 | + if ur == 0: |
| 90 | + print("Upper Right = On") |
| 91 | + else: print("Upper Right = Off") |
| 92 | + builtin_led.toggle() |
| 93 | + ur_old = ur |
| 94 | + counter += 1 |
| 95 | + |
| 96 | + if lr != lr_old: |
| 97 | + if lr == 0: |
| 98 | + print("Lower Right = On") |
| 99 | + else: print("Lower Right = Off") |
| 100 | + builtin_led.toggle() |
| 101 | + lr_old = lr |
0 commit comments