#Altera estado sem usar sleep
#reincia ao estado zero quanto pressiona o botão
# Botão com debounce
from machine import Pin
from utime import ticks_ms
bt1 = Pin(4,Pin.IN, Pin.PULL_UP)
states = ("state 0", "state 1", "state 2", "state 3")
index = 0
initial_time = 0
debounce_time = 500
last_press_time = ticks_ms()
def get_button(bt, last_press_time, debounce_time ):
current_time = ticks_ms()
if bt.value() == 0 and current_time - last_press_time > debounce_time:
return True, current_time
else:
return False, last_press_time
while True:
if ticks_ms() - initial_time > 5000 or index == 0:
print("Estado atual: {}".format(states[index]))
index = index +1
initial_time = ticks_ms()
bt_state, last_press_time = get_button(bt1, last_press_time, debounce_time)
if bt_state or index == 4:
index = 0