# Importe os módulos necessários
import machine
import time
# Defina os pinos
PIN_SENSOR = 2
PIN_BUZZER = 5
PIN_LED = 4
# Configuração dos pinos
machine.Pin(PIN_SENSOR, machine.Pin.IN)
machine.Pin(PIN_BUZZER, machine.Pin.OUT)
machine.Pin(PIN_LED, machine.Pin.OUT)
def play_tone(frequency, duration_ms):
machine.freq(160000000) # Ajuste a frequência do clock para 160 MHz
buzzer = machine.PWM(machine.Pin(PIN_BUZZER))
buzzer.freq(frequency)
buzzer.duty(512) # Ajuste o volume (0-1023)
time.sleep_ms(duration_ms)
buzzer.deinit()
while True:
movimento = machine.Pin(PIN_SENSOR).value()
if movimento:
play_tone(523, 2000) # Toca o tom C5 (523 Hz)
machine.Pin(PIN_LED).on()
time.sleep_ms(500)
machine.Pin(PIN_LED).off()
time.sleep_ms(500)
play_tone(523, 2000) # Toca o tom C5 (523 Hz) novamente
machine.Pin(PIN_LED).on()
time.sleep_ms(500)
else:
machine.Pin(PIN_BUZZER).off()
machine.Pin(PIN_LED).off()