import library_ultrasonic
from machine import Pin
from utime import sleep
import library_servor
# Import the 'time' module
import time
# Declare pins
TRIG = Pin(12, Pin.OUT) # Changed Pin.OUT to Pin.OUT for TRIG
ECHO = Pin(14, Pin.IN) # Changed Pin.IN to Pin.IN for ECHO
Servo_pin = Pin(33, Pin.OUT)
# Declare LED
Led_merah = Pin(2, Pin.OUT)
# Declare buzzer
buzzer_pin = Pin(21, Pin.OUT)
# Create the object for the sensor with the library
put_jauh = library_ultrasonic.HCSR04(trigger_pin=TRIG, echo_pin=ECHO)
tiang_gate = library_servor.Servo(pin=33)
# Function to activate the buzzer for a short duration
def activate_buzzer():
buzzer_pin.on()
time.sleep(0.5) # Buzzer on for 0.5 seconds
buzzer_pin.off()
time.sleep(0.5) # Wait for 0.5 seconds before next activation
# Main program
while True:
Led_merah.on()
sleep(2)
print("\n=======JOM CHECK INCOMING DISTANCE========\n")
distance_in_mm = put_jauh.distance_mm()
print("objek menghampiri dalam:", distance_in_mm, "mm")
distance_in_cm = put_jauh.distance_cm()
print("objek menghampiri dalam:", distance_in_cm, "cm")
if distance_in_cm < 400:
tiang_gate.move()
sleep(0.2)
tiang_gate.move(90)
sleep(5)
tiang_gate.move(0)
sleep(1)
else:
tiang_gate.move(0)
#Servor motor part
tiang_gate.move(0)
sleep(0.1)
tiang_gate.move(90)
sleep(5)
tiang_gate.move(0)
sleep(1)
# Call the function repeatedly to make the buzzer produce a longer sound
for i in range(5): # Repeat 5 times for a 5-second sound
activate_buzzer()
# Turn off the LED
Led_merah.off()
sleep(2) # In every 2 sec, ultrasonic will sense new incoming distance