from machine import Pin, PWM
from utime import sleep_ms
from picozero import Speaker
pir = Pin(17, Pin.IN)
led = Pin(14, Pin.OUT)
botao = Pin(15, Pin.IN)
buzzer = Speaker(16)
estado_botao = False
ultimo_estado_botao = False
def verificar_botao():
global ultimo_estado_botao, estado_botao
if ultimo_estado_botao == False and botao.value() == 1:
estado_botao = not estado_botao
ultimo_estado_botao = True
elif botao.value() == 0 and ultimo_estado_botao == True:
ultimo_estado_botao = False
return estado_botao
ultimo_estado = False
while True:
sleep_ms(10)
if verificar_botao():
if ultimo_estado == False:
print("🟢 Sistema ligado")
ultimo_estado = True
if pir.value():
led.on()
buzzer.on()
else:
led.off()
buzzer.off()
else:
if ultimo_estado == True:
ultimo_estado = False
print("🔴 Sistema desligado")
led.off()
buzzer.off()