from machine import Pin
import time
button = Pin(1, Pin.IN, Pin.PULL_DOWN)
red = Pin(0, Pin.OUT)
green = Pin(2, Pin.OUT)
yellow = Pin(3, Pin.OUT)
press_count = 0
last_press = 0
WAIT_WINDOW = 1000
button_state = 0
def toggle(pin):
pin.value(not pin.value())
while True:
now = time.ticks_ms()
reading = button.value()
if reading == 1 and button_state == 0:
press_count += 1
last_press = now
print("PRESS:", press_count)
button_state = reading
if press_count > 0 and time.ticks_diff(now, last_press) > WAIT_WINDOW:
print("EXECUTE:", press_count)
if press_count == 1:
toggle(red)
elif press_count == 2:
toggle(green)
elif press_count == 3:
toggle(yellow)
press_count = 0
time.sleep(0.003)