from machine import Pin
from time import sleep_ms
from ultrassonico import medeDistancia
#Ajusto o relógio
trigger = Pin(22, Pin.OUT)
echo = Pin(21, Pin.IN)
bot = Pin(26, Pin.IN, Pin.PULL_DOWN) # Precisa do PULL_DOWN para não flutuar
bAnterior = bot.value() # Representa o estado anterior do botão (antes de pressionar ou soltar)
while True:
bAtual = bot.value() # Verifica o estado atual do botão
if bAtual != bAnterior: # O estado do botão mudou?
if bAtual == 1: # O estado mudou porque o botão foi pressionado? Execute a ação
# atualiza data/hora (ts)
dist = medeDistancia(trigger, echo)
if dist is not None:
print(f"Distância:{dist*10:.2f}mm")
else:
print('Medição não foi possível')
bAnterior = bAtual # Atualiza bAnterior para detectar futuras mudanças
sleep_ms(100) # Para evitar os efeitos do bouncing