from hcsr04 import HCSR04
from machine import Pin, PWM
import time
sensor = HCSR04(trigger_pin=12, echo_pin=14, echo_timeout_us=1000000)
ledR = Pin(4,Pin.OUT)
ledG = Pin(5,Pin.OUT)
buzzer = PWM(Pin(2), freq=440, duty=512)
def SoundBuz(freq, sleep):
buzzer.freq(freq)
buzzer.duty(512)
time.sleep(sleep)
while True:
distance = sensor.distance_cm()
if distance > 200 :
ledG.value(1)
ledR.value(0)
time.sleep(1)
else :
ledG.value(0)
ledR.value(1)
SoundBuz(440, 0.25)
time.sleep(1)