from machine import Pin
from utime import sleep
B_led = Pin(0,Pin.OUT)
G_led = Pin(1,Pin.OUT)
R_led = Pin(2,Pin.OUT)
button = Pin(3, Pin.IN , Pin.PULL_DOWN) # Pull-down button
history = 0 # 8-bit history (starts at 0)
pressed = False
def light_system(blue , green , red):
B_led.value(blue)
G_led.value(green)
R_led.value(red)
def main():
global button
sleep(3)
a = button.value()
light_system(a , a , a)
button.irq(trigger=machine.Pin.IRQ_FALLING, handler = interrupt)
def interrupt():
global history
global pressed
while True:
history = history << 1 | button.value() # Shift in the current reading
history = history & 0b11111111 # Keep only 8 bits
# Check if the last 8 samples were all 1s (button held down)
if history == 0b11111111 and not pressed:
print("Button pressed")
pressed = True
elif history == 0b00000000 and pressed:
pressed = False
if __name__ == "__main__":
main()
interrupt()